C++ 通过模板跟踪编译器错误

C++ 通过模板跟踪编译器错误,c++,templates,C++,Templates,此代码段的最后一行有输入错误,缺少限定符::type template<bool ci> struct comp { typedef ci_compare_string type; }; template< > struct comp<false> { typedef std::less<std::string> type; }; template <typename T, bool ci = true&

此代码段的最后一行有输入错误,缺少限定符::type

template<bool ci> struct comp        { typedef ci_compare_string      type; };
template<       > struct comp<false> { typedef std::less<std::string> type; };

template <typename T, bool ci = true>  //map w str keys, case sensitive option
struct mapx : std::map<std::string, T, typename comp<ci> > {};  // oops, should be comp_<ci>::type
VS2008编译器在下面显示的std::map源代码行报告了错误。 消息was term未计算为包含2个参数的函数

...
mapped_type& operator[](const key_type& _Keyval)
    {   // find element matching _Keyval or insert with default mapped
    iterator _Where = this->lower_bound(_Keyval);
    if (_Where == this->end()
        || this->comp(_Keyval, this->_Key(_Where._Mynode())))   <=== ERROR !!!!
        _Where = this->insert(_Where,
            value_type(_Keyval, mapped_type()));
    return ((*_Where).second);
    }
};
...
所以最终我发现这个错误一定与比较器有关,然后我一直盯着看,直到我意识到我忘了键入::type

我以前没有使用过太多的w模板,我想知道跟踪像这样的编译器错误的正确方法。在这种情况下应该使用什么技巧/技巧

消息was term未计算为包含2个参数的函数

在VisualStudio中,错误列表仅显示任何错误消息的第一行。这是一个易于浏览的错误摘要。有些错误消息很长,特别是当涉及模板时。完整的错误消息可以在生成的输出窗口中找到

当模板实例化过程中发生错误时,编译器将打印检测到错误时正在实例化的模板堆栈。例如,当我用Visual C++ 2012编译你的代码片段时,它打印了以下错误:VisualC++ 2008将打印一个类似的消息,尽管它将有不同的标准库实现:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1792) : error C2064: term does not evaluate to a function taking 2 arguments
        C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1153) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::_Insert_nohint<std::pair<const _Kty,_Ty>&,std::_Tree_node<_Value_type,_Voidptr>*>(bool,_Valty,_Nodety)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Kty=std::string,
            _Ty=int,
            _Value_type=std::pair<const std::string,int>,
            _Voidptr=void *,
            _Valty=std::pair<const std::string,int> &,
            _Nodety=std::_Tree_node<std::pair<const std::string,int>,void *> *
        ]
        C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1153) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::_Insert_nohint<std::pair<const _Kty,_Ty>&,std::_Tree_node<_Value_type,_Voidptr>*>(bool,_Valty,_Nodety)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Kty=std::string,
            _Ty=int,
            _Value_type=std::pair<const std::string,int>,
            _Voidptr=void *,
            _Valty=std::pair<const std::string,int> &,
            _Nodety=std::_Tree_node<std::pair<const std::string,int>,void *> *
        ]
        stubby.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert<std::pair<const char *,int>>(_Valty &&)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Valty=std::pair<const char *,int>
        ]
        stubby.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert<std::pair<const char *,int>>(_Valty &&)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Valty=std::pair<const char *,int>
        ]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1796) : error C2064: term does not evaluate to a function taking 2 arguments
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1817) : error C2064: term does not evaluate to a function taking 2 arguments
现在,从这些术语的任何定义来看,这仍然不是特别容易阅读或用户友好的。但它确实显示了错误发生的位置。三个顶级错误发生在的第1792、1796和1817行,所有这些行都试图使用比较器比较两个参数

...
mapped_type& operator[](const key_type& _Keyval)
    {   // find element matching _Keyval or insert with default mapped
    iterator _Where = this->lower_bound(_Keyval);
    if (_Where == this->end()
        || this->comp(_Keyval, this->_Key(_Where._Mynode())))   <=== ERROR !!!!
        _Where = this->insert(_Where,
            value_type(_Keyval, mapped_type()));
    return ((*_Where).second);
    }
};
...
消息was term未计算为包含2个参数的函数

在VisualStudio中,错误列表仅显示任何错误消息的第一行。这是一个易于浏览的错误摘要。有些错误消息很长,特别是当涉及模板时。完整的错误消息可以在生成的输出窗口中找到

当模板实例化过程中发生错误时,编译器将打印检测到错误时正在实例化的模板堆栈。例如,当我用Visual C++ 2012编译你的代码片段时,它打印了以下错误:VisualC++ 2008将打印一个类似的消息,尽管它将有不同的标准库实现:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1792) : error C2064: term does not evaluate to a function taking 2 arguments
        C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1153) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::_Insert_nohint<std::pair<const _Kty,_Ty>&,std::_Tree_node<_Value_type,_Voidptr>*>(bool,_Valty,_Nodety)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Kty=std::string,
            _Ty=int,
            _Value_type=std::pair<const std::string,int>,
            _Voidptr=void *,
            _Valty=std::pair<const std::string,int> &,
            _Nodety=std::_Tree_node<std::pair<const std::string,int>,void *> *
        ]
        C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1153) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::_Insert_nohint<std::pair<const _Kty,_Ty>&,std::_Tree_node<_Value_type,_Voidptr>*>(bool,_Valty,_Nodety)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Kty=std::string,
            _Ty=int,
            _Value_type=std::pair<const std::string,int>,
            _Voidptr=void *,
            _Valty=std::pair<const std::string,int> &,
            _Nodety=std::_Tree_node<std::pair<const std::string,int>,void *> *
        ]
        stubby.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert<std::pair<const char *,int>>(_Valty &&)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Valty=std::pair<const char *,int>
        ]
        stubby.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert<std::pair<const char *,int>>(_Valty &&)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Valty=std::pair<const char *,int>
        ]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1796) : error C2064: term does not evaluate to a function taking 2 arguments
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1817) : error C2064: term does not evaluate to a function taking 2 arguments

现在,从这些术语的任何定义来看,这仍然不是特别容易阅读或用户友好的。但它确实显示了错误发生的位置。三个顶级错误出现在的第1792、1796和1817行,所有这些行都试图使用比较器来比较两个参数。

通常,Clang 3.2有更好的错误报告,可能也可以尝试在您的计算机上设置,当VS抛出一些模糊的内容时,请使用Clang进行双重检查。当然,除非您依赖于MS的某些特定功能,否则一个有趣的练习就是试图破译编译器告诉您的内容。如果您发布错误消息,我们可能会帮助您理解它所说的内容。在很多情况下,理解编译器告诉你的是一门艺术。通常Clang3.2有更好的错误报告,也许也可以尝试在你的机器上设置它,当VS抛出一些模糊的东西时,请仔细检查Clang。当然,除非您依赖于MS的某些特定功能,否则一个有趣的练习就是试图破译编译器告诉您的内容。如果您发布错误消息,我们可能会帮助您理解它所说的内容。在许多情况下,理解编译器告诉您的内容是一门艺术。
...
mapped_type& operator[](const key_type& _Keyval)
    {   // find element matching _Keyval or insert with default mapped
    iterator _Where = this->lower_bound(_Keyval);
    if (_Where == this->end()
        || this->comp(_Keyval, this->_Key(_Where._Mynode())))   <=== ERROR !!!!
        _Where = this->insert(_Where,
            value_type(_Keyval, mapped_type()));
    return ((*_Where).second);
    }
};
...