使用模板:操作符首先解析还是首先转换解析? [英] With templates: Operator resolved first or conversion resolved first?

查看:288
本文介绍了使用模板:操作符首先解析还是首先转换解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天看到一些有趣的编译器行为,我想我明白为什么会发生,但我想确定。所以,我不会写我的推理,只是事实。

I saw some interesting compiler behavior yesterday and I think I understand why it's happening, but I want to be sure. So, I'm not going to write my reasoning, just the facts.

注意,这不是一个错字,我包括 vector 而不是 string 。我有意这样做,使编译器不能理解什么是std ::字符串,所以它必须搜索周围找出由 + 引用的操作符, code>:

Note, it's not a typo that I've included vector instead of string. I did it intentionally so that the compiler would not be able to understand what an std::string was and so that it would have to search around to figure out which operator I'm referring to by +:

#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中遇到了一大堆编译器错误。只显示它们的子集。

So, I get a slew of compiler errors in MS Visual Studio 2005. I'm only showing a subset of them.

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 +'

...错误继续...

... errors continued ...

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 +'

...错误继续...

... errors continued ...

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 +'

...错误继续...

... errors continued ...

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 +'

...错误继续...

... errors continued ...

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 +'

因此,编译器搜索 + 意味着什么,并抱怨它不能推断适当的模板参数。我感到惊讶的两个相关的事情,是它给每个重载的 + 运算符涉及模板的这个错误。这告诉我编译器绝对没有办法排除这些 + 没有意义; 两个,这是相关的,它不只是抱怨没有合适的运算符存在。

So, the compiler searches around for the what the + means and complains that it can't deduce the appropriate template arguments. I'm surprised by two related things, one is that it gives this error for each overloaded + operator that involves templates. This tells me that the compiler has absolutely no way of ruling out that any of these + are not meaningful; two, which is related, that it doesn't just complain that no suitable operator exists.

我认为这是一个机会,关于如何实例化和编译模板。

I see this as an opportunity to learn something about how templates are instantiated and compiled. Does anyone have any good explanation or references?

推荐答案

事实上,由于找不到匹配,它使用错误告诉你实际找到的可能的运算符候选。

In fact, since it couldn't find a match it's using the errors to tell you the possible operator candidates it actually found.

例如,g ++错误告诉你它找不到运算符,相同的消息,但在candidate are:形式,而不是每个操作符一个错误。

The g++ error for example tells you that it can't find the operator and then provides the same slew of messages, but in "candidates are:" form rather than one error per operator.

这是为了试图有所帮助:找到匹配,假设您可能实际上意味着任何可能的可用操作符。

It's doing this in an attempt to be helpful: Since it couldn't find a match it assumes you may have actually meant any of the possible available operators.

这篇关于使用模板:操作符首先解析还是首先转换解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆