实例化模板函数时出现奇怪的错误

时间:2012-07-06 15:04:50

标签: c++ templates

我有以下模板功能:

template <std::size_t first, std::size_t last, typename T>
bool in_range(T& in)
{
    for(auto i = in.begin(); i!=in.end(); ++i)
        if(*i<first || *i>last)
            return false;
    return true;
}

但是当我尝试使用它时:

std::vector<int> test;
test.push_back(1);
test.push_back(5);
test.push_back(6);

std::cout<<in_range<4,7>(test);

我得到了这个奇怪的错误:

main.cpp: In instantiation of 'bool in_range(T&) [with long long unsigned int first = 4ull; long long unsigned int last = 7ull; T = std::vector<int>]':
main.cpp:31:34:   required from here

我做错了什么?

编辑:完整构建日志:http://pastebin.com/Cwemq2Hk

1 个答案:

答案 0 :(得分:2)

如果我在启用C ++ 11支持的情况下构建它,那么它会编译。 Here is a demonstation

在C ++ 11之前,auto具有不同的含义,因此auto i = ...无效 - 它声明了一个没有类型的变量。

我猜你正在使用GCC;根据版本的不同,您需要指定-std=c++0x-std=c++11作为命令行选项。

相关问题