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

标签 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>  //map w str keys, case sensitive option
struct mapx : std::map<std::string, T, typename comp<ci> > {};  // oops, should be comp_<ci>::type

VS 2008 编译器在 std::map 源代码行报告了错误,如下所示。 该消息是“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”。

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

最佳答案

The message was "term does not evaluate to a function taking 2 arguments"

在 Visual Studio 中,错误列表仅显示任何错误消息的第一行。它旨在成为易于浏览的错误摘要。有些错误消息很长,尤其是涉及模板时。完整的错误消息可以在构建的输出窗口中找到。

当模板实例化期间发生错误时,编译器将打印检测到错误时正在实例化的模板堆栈。例如,当我使用 Visual C++ 2012 编译您的代码片段时,它会打印以下错误(Visual C++ 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

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

关于c++ - 通过模板跟踪编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15034522/

相关文章:

c++ - 不必使用 boolean 值和模板参数列出所有可能性

c++ - GTk3在windows上开发

c++ - 没有对模板函数的匹配函数调用

java - Python Web 框架与 Java Web 框架(Python 中的 Web 开发是如何完成的?)

c++ - 为什么下面的非静态数据成员初始化在C++11中是无效的

c++ - 从模板特化中获取对类成员的访问

c++ - 在初始化列表之前执行检查

c++ - QLabel 没有正确更新

c++ - C++代码中的 “Undefined reference to operator<<”错误

c++ - 如何在 0 个可变参数上专门化可变参数模板类?