实例化模板函数时出现奇怪错误
Weird error when instantiating template function
我有以下模板函数:
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
如果我在启用C++11支持的情况下构建它,那么它就会编译。这是一个演示。
在C++11之前,auto
有不同的含义,因此auto i = ...
是无效的——它声明了一个没有类型的变量。
我猜你使用的是GCC;根据版本的不同,您需要指定-std=c++0x
或-std=c++11
作为命令行选项。
相关文章:
- 链接器错误:函数的多个定义
- 编译器错误:函数调用在常量表达式中必须有一个常量值
- 错误:函数声明符之后的预期函数体
- C 错误 - 函数不能超载
- 如何修复传递参数时调用错误函数的主函数?(C++)
- 错误:函数调用中有两个参数
- 错误:函数不是“类”的静态数据成员 - C++
- C ,G 编译错误函数
- CMake 解析错误函数缺少结尾")"。而是找到带有文本的未终止字符串")
- C++ 错误 函数 2 的多重定义
- 错误:函数调用中从int到int(*)[4]的转换无效
- C++14 自动扣除错误:函数返回一个数组
- 视觉C++错误:函数必须返回一个值
- 错误:函数声明中的两种或多种数据类型
- 错误:函数未在作用域中声明
- rtw_android.c错误:函数“strnicmp”的隐式声明[-Weror=隐式函数声明]
- 错误:函数中的return语句没有值,返回“void*”[-fpermission]
- 简单程序中的链接器错误:函数的多重定义
- 错误:函数参数太多
- 错误:函数__tmaincrtstartup中引用的未解析的外部符号_main