意外的模板实例化导致编译错误
Unexpected template instantiation leading to a compile error
我只是想知道为什么我的代码无法编译。下面的可以吗?我试图用Category1和Category2typedef-s声明一个简单的类。
类别1 typedef编译良好,但类别2则不然。
似乎无法编译Category2 typedef,因为类迭代器_traits<>被实例化,尽管周围的类X没有被实例化。。。对我来说似乎很困惑。
#include <iterator>
template <class GetterFunType>
struct X {
GetterFunType containerGetterFun;
// works
typedef typename std::iterator_traits<typename GetterFunType::iterator>::iterator_category Category1;
// compile error - std::iterator_traits<> is instantiated with type 'unknown'
typedef typename std::iterator_traits<
decltype(containerGetterFun().begin())>::iterator_category Category2;
X(GetterFunType _containerGetterFun) : containerGetterFun(_containerGetterFun) { }
};
注意,我不需要实例化类X来获得以下错误(以上是完整的编译单元)。
在Visual Studio 2012中,我得到了以下内容:
1>c:program files (x86)microsoft visual studio 11.0vcincludexutility(364): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1> c:datafslapif_src_reviewapif_srcsystemns.cpp(11) : see reference to class template instantiation 'std::iterator_traits<_Iter>' being compiled
1> with
1> [
1> _Iter=unknown
1> ]
1> c:datafslapif_src_reviewapif_srcsystemns.cpp(14) : see reference to class template instantiation 'X<GetterFunType>' being compiled
1>c:program files (x86)microsoft visual studio 11.0vcincludexutility(364): error C3254: 'std::iterator_traits<_Iter>' : class contains explicit override 'iterator_category' but does not derive from an interface that contains the function declaration
1> with
1> [
1> _Iter=unknown
1> ]
1>c:program files (x86)microsoft visual studio 11.0vcincludexutility(364): error C2838: 'iterator_category' : illegal qualified name in member declaration
1>c:program files (x86)microsoft visual studio 11.0vcincludexutility(364): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:program files (x86)microsoft visual studio 11.0vcincludexutility(364): error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'
1> with
1> [
1> _Iter=unknown
1> ]
1> c:program files (x86)microsoft visual studio 11.0vcincludexutility(364) : see declaration of 'std::iterator_traits<_Iter>::iterator_category'
1> with
1> [
1> _Iter=unknown
1> ]
1>c:program files (x86)microsoft visual studio 11.0vcincludexutility(364): error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name
1> with
1> [
1> _Iter=unknown
1> ]
在xutility(364)中有:
template<class _Iter>
struct iterator_traits
{ // get traits from iterator _Iter
typedef typename _Iter::iterator_category iterator_category;
typedef typename _Iter::value_type value_type;
typedef typename _Iter::difference_type difference_type;
typedef difference_type distance_type; // retained
typedef typename _Iter::pointer pointer;
typedef typename _Iter::reference reference;
};
我的情况是,我想声明一个在构造函数中获得lambda的类。lambda应返回对容器的引用。我需要确定返回的容器是否有一个随机访问迭代器。但我被这个编译错误卡住了。谢谢你的解释!
我能够使用gcc 5.3.1编译相同的代码,没有任何错误,带有-std=c++11
您的编译器是一个相对较旧的编译器,不支持当前的C++1x标准。如果您需要使用现代C++功能,切换到另一个编译器是我在这里看到的唯一选项。
我看到代码还可以,但有一些Visual Studio 2012的限制,不允许编译。至少在MSVS 2015中,它是有效的,与gcc 5.3.1相同。谢谢,朋友们!
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?