通过模板跟踪编译器错误
tracing compiler errors through 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
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行,所有这些行都试图使用比较器来比较两个参数。
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 编译器错误:destuctor 的更宽松的抛出说明符
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++ G++ 编译器 - 错误:隐式声明的定义
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 模板模板参数和模板别名:编译器错误?
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 编译器错误(英特尔并行工作室 2019 与 Visual Studio 社区 2019)
- 如何在C++中克服此 C4430 编译器错误?
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- C++自定义分配器大小参数作为模板参数会引发编译器错误