为什么添加两个字符串文字不使用operator +(const string&amp ;, const string&)? [英] Why does adding two string literals not use operator+(const string&, const string&)?

查看:205
本文介绍了为什么添加两个字符串文字不使用operator +(const string&amp ;, const string&)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将帖子重新格式化以使其更加清晰.

I have reformatted the post to be clearer.

为什么这样做:

struct A {};

struct B {
    B(A){}
};

void operator+(const B&, const B&) {}

int main()
{
    A a1, a2;
    a1 + a2;
}

这不是吗?

struct B {
    B(const char*){}
};

void operator+(const B&, const B&) {} //error: invalid operands of types 'const char [6]' and 'const char [6]' to binary 'operator+'|

int main()
{
    "Hello" + "world";
}

本质上,在第一个示例中,a1a2都通过隐式转换转换为B对象,并使用operator+(const B&, const B&)添加.

Essentially, in the first example a1 and a2 both convert to B objects through the implicit conversion and use the operator+(const B&, const B&) to add.

在此示例之后,我希望"Hello""world"再次通过隐式构造函数转换为B对象,并使用operator+(const B&, const B&)相互添加.而是有一个错误,指示C样式字符串未尝试将用户定义的转换为B进行添加.为什么是这样?有防止这种情况发生的基本属性吗?

Following from this example, I would have expected "Hello" and "world" to convert to B objects, again through the implicit constructor, and use operator+(const B&, const B&) to add to each other. Instead there is an error, which indicates the C-style strings do not attempt a user-defined conversion to B in order to add. Why is this? Is there a fundamental property that prevents this?

推荐答案

在您的第一个示例中,允许重载解析找到您的operator+:

In your first example, overload resolution is allowed to find your operator+:

[C++14: 13.3.1.2/2]: 如果任何一个操作数的类型是类或枚举,则可能会声明一个用户定义的运算符函数来实现该运算符,或者可能需要用户定义的转换才能实现将操作数转换为适合于内置运算符的类型. 在这种情况下,重载解析用于确定要调用哪个运算符功能或内置运算符来实现该运算符. [..]

[C++14: 13.3.1.2/2]: If either operand has a type that is a class or an enumeration, a user-defined operator function might be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator. In this case, overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator. [..]

[C++14: 13.3.2/1]: 从为给定上下文(13.3.1)构建的一组候选函数中,选择了一组可行函数,通过比较参数转换从中选择最佳函数最佳拟合顺序(13.3.3).可行函数的选择要考虑参数和函数参数之间的关系,而不是转换序列的排名.

[C++14: 13.3.2/1]: From the set of candidate functions constructed for a given context (13.3.1), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences for the best fit (13.3.3). The selection of viable functions considers relationships between arguments and function parameters other than the ranking of conversion sequences.

[C++14: 13.3.2/2]:首先,要成为可行的功能,候选功能应具有足够的参数,以在数量上与列表中的参数一致.

  • 如果列表中有m个参数,则所有具有完全m个参数的候选函数都是可行的.
  • [..]
  • If there are m arguments in the list, all candidate functions having exactly m parameters are viable.
  • [..]

[C++14: 13.3.2/3]:第二,要使F是可行的函数,每个自变量都应存在一个隐式转换序列(13.3.3.1),该序列将该自变量转换为自变量. F的相应参数. [..]

[C++14: 13.3.2/3]: Second, for F to be a viable function, there shall exist for each argument an implicit conversion sequence (13.3.3.1) that converts that argument to the corresponding parameter of F. [..]

(您可以自己检查隐式转换序列"的措辞,以发现允许使用operator+调用;规则过于冗长,无法在此处逐字复制.)

(You may examine the wording for "implicit conversion sequence" yourself to see that the operator+ call is permissible; the rules are too verbose to warrant verbatim reproduction here.)

但是,在您的第二个示例中,重载解析受限于一种基本的算术加法机制(未为const char[N]const char*定义的一种),有效地禁止了考虑任何operator+函数:

However, in your second example, overload resolution is constrained to a basic arithmetic addition mechanism (one which is not defined for const char[N] or const char*), effectively prohibiting any operator+ function from being considered:

[C++14: 13.3.1.2/1]: 如果表达式中没有任何运算符的操作数具有类或枚举类型,则假定该运算符是内置运算符,并根据第5章进行解释.

[C++14: 5.7/1]: [..] 另外,两个操作数都应具有算术或无作用域枚举类型,或者一个操作数应是指向完全定义的指针对象类型,另一个应具有整数或无范围的枚举类型. [..]

[C++14: 5.7/1]: [..] For addition, either both operands shall have arithmetic or unscoped enumeration type, or one operand shall be a pointer to a completely-defined object type and the other shall have integral or unscoped enumeration type. [..]

[C++14: 5.7/3]:二进制+运算符的结果是操作数之和.

[C++14: 5.7/3]: The result of the binary + operator is the sum of the operands.

这篇关于为什么添加两个字符串文字不使用operator +(const string&amp ;, const string&)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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