昨天我看到了一些有趣的编译器行为,我想我理解它为什么会发生,但我想确定。所以,我不会写我的推理,只是事实。使用模板:首先解析运算符还是首先解析转换?
请注意,这不是我输入的错别字vector
而不是string
。我这样做是故意这样编译器就无法理解一个的std :: string是等它不得不四处搜寻找出我被+
指哪运营商:
#include <vector>
// #include <string> // intentionally commented out
template <typename T> struct A
{
A() { };
~A() { };
int m_member;
};
template <typename T> A<T> operator+(double lhs, const A<T> &rhs);
int main(int argc, char **argv)
{
std::string fullString = std::string("Hi ") + std::string("mom!");
}
所以,我在MS Visual Studio 2005中遇到了一些编译器错误。我只显示了它们的一个子集。
1>.\test.cpp(21) : error C2784: 'A<T> operator +(double,const A<T> &)' : could not deduce template argument for 'const A<T> &' from 'std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> .\test.cpp(16) : see declaration of 'operator +'
...继续错误...
1>.\test.cpp(21) : error C2784: 'std::_Vb_iterator<_MycontTy> std::operator +(_Vb_iterator<_MycontTy>::difference_type,std::_Vb_iterator<_MycontTy>)' : could not deduce template argument for 'std::_Vb_iterator<_MycontTy>' from 'std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\vector(1800) : see declaration of 'std::operator +'
...错误继续...
1>.\test.cpp(21) : error C2784: 'std::_Vb_const_iterator<_MycontTy> std::operator +(_Vb_const_iterator<_MycontTy>::difference_type,std::_Vb_const_iterator<_MycontTy>)' : could not deduce template argument for 'std::_Vb_const_iterator<_MycontTy>' from 'std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\vector(1695) : see declaration of 'std::operator +'
...错误继续...
1>.\test.cpp(21) : error C2784: 'std::_Vector_iterator<_Ty,_Alloc> std::operator +(_Vector_iterator<_Ty,_Alloc>::difference_type,std::_Vector_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_iterator<_Ty,_Alloc>' from 'std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\vector(396) : see declaration of 'std::operator +'
...错误继续...
1>.\test.cpp(21) : error C2784: 'std::_Vector_const_iterator<_Ty,_Alloc> std::operator +(_Vector_const_iterator<_Ty,_Alloc>::difference_type,std::_Vector_const_iterator<_Ty,_Alloc>)' : could not deduce template argument for 'std::_Vector_const_iterator<_Ty,_Alloc>' from 'std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include\vector(264) : see declaration of 'std::operator +'
因此,编译器四处搜索+
意味着什么,并抱怨说它无法推断出适当的模板参数。我惊讶于两件相关的事情,其中一个是它为每个涉及模板的超载+
运算符提供此错误。这告诉我,编译器绝对没有办法排除这些+
中的任何一个都没有意义; 两个,这是相关的,它不只是抱怨没有合适的运营商存在。
我认为这是一个学习如何实例化和编译模板的机会。有没有人有任何好的解释或参考?
哦,并且只是为了编译器甚至知道'std :: basic_string <...>'的评论。它包含在''通过''通过''。 :) –
Xeo
2011-04-28 17:16:14
+1这很有趣。我也想知道。 – 2011-04-28 17:16:52