意外的模板实例化导致编译错误

Unexpected template instantiation leading to a compile error

本文关键字:编译 错误 实例化 意外      更新时间:2023-10-16

我只是想知道为什么我的代码无法编译。下面的可以吗?我试图用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相同。谢谢,朋友们!