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

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

问题描述

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

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+?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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