昨天我看到了一些有趣的編譯器行爲,我想我理解它爲什麼會發生,但我想確定。所以,我不會寫我的推理,只是事實。使用模板:首先解析運算符還是首先解析轉換?
請注意,這不是我輸入的錯別字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