通过模板跟踪编译器错误

tracing compiler errors through templates

本文关键字:编译器 错误 跟踪      更新时间:2023-10-16

这个片段的最后一行有一个拼写错误(缺少限定符: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源行报告了错误。消息是"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模板,我想知道追踪像这样的编译器错误的正确方法。在这种情况下应该使用什么技巧?

消息是"术语不计算为接受2个参数的函数"

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

当模板实例化过程中发生错误时,编译器将打印检测到错误时正在实例化的模板堆栈。例如,当我使用Visual C++2012编译您的代码片段时,它会打印以下错误(Visual C++2008会打印类似的消息,但由于标准库实现的差异,它必然会有所不同):

C:Program Files (x86)Microsoft Visual Studio 11.0VCINCLUDExtree(1792) : error C2064: term does not evaluate to a function taking 2 arguments
        C:Program Files (x86)Microsoft Visual Studio 11.0VCINCLUDExtree(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.0VCINCLUDExtree(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.0VCINCLUDExtree(1796) : error C2064: term does not evaluate to a function taking 2 arguments
C:Program Files (x86)Microsoft Visual Studio 11.0VCINCLUDExtree(1817) : error C2064: term does not evaluate to a function taking 2 arguments

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