【发布时间】:2011-04-28 17:05:40
【问题描述】:
我昨天看到了一些有趣的编译器行为,我想我理解它为什么会发生,但我想确定一下。所以,我不打算写我的推理,只写事实。
请注意,我使用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<...>。它包含在<vector>通过<xfunctional>通过<xstring>。 :) -
+1 这很有趣。我也想知道。
标签: c++ templates compiler-construction compiler-errors