gpt4 book ai didi

比较运算符重载与转换运算符的 C++ 优先级

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:54 24 4
gpt4 key购买 nike

考虑以下程序:

#include <iostream>

using namespace std;

class Foo {
public:
int k;

operator int() {
cout << "convert int" << endl;
return k;
}

#if USE_COMPARE
bool operator < (int rhs) {
cout << "compare with" << endl;
return (k < rhs);
}
#endif
};

int main()
{
Foo f;
f.k = 3;
int m = 5;


if (f < m) {
cout << 1 << endl;
return 1;
}
cout << 0 << endl;
return 0;
}

USE_COMPARE定义,if (f<m)的比较将使用比较运算符重载。如果USE_COMPARE未定义,它将转换 f来自 Fooint ,然后进行整数比较。在我看来,比较运算符重载的优先级高于转换运算符。任何人都可以从 C++ 标准的角度确认这一点吗?

但我认为比较运算符应该具有优先权是自然的。但请从C++标准的角度回答问题。

谢谢。

最佳答案

13.3.3.2/2

When comparing the basic forms of implicit conversion sequences (as defined in 13.3.3.1)

a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined con- version sequence or an ellipsis conversion sequence, and

a user-defined conversion sequence (13.3.3.1.2) is a better conversion sequence than an ellipsis conver- sion sequence (13.3.3.1.3).

13.3.3.1/3

A well-formed implicit conversion sequence is one of the following forms: — a standard conversion sequence (13.3.3.1.1),

— a user-defined conversion sequence (13.3.3.1.2), or

— an ellipsis conversion sequence (13.3.3.1.3).

13.3.3.1/8

If no conversions are required to match an argument to a parameter type, the implicit conversion sequence is the standard conversion sequence consisting of the identity conversion (13.3.3.1.1).

13.3.3.1.2/1

A user-defined conversion sequence consists of an initial standard conversion sequence followed by a user- defined conversion (12.3) followed by a second standard conversion sequence. If the user-defined conversion is specified by a conversion function (12.3.2), the initial standard conversion sequence converts the source type to the implicit object parameter of the conversion function.

如果定义了比较运算符,编译器有两种变体:

1)让

IF = Identity(f)

调用:

IF.operator <(int)

2)让:

IF = Identity(f);
converted_int = Identity(IF.operator int());

调用:

operator < (converted_int, int);

隐式转换序列优于用户转换序列。标准中有两个关于引号重载解析的词,如果你愿意,你可以阅读 par 13.3,或者只是 13.3.3[over.best.ics]。

关于比较运算符重载与转换运算符的 C++ 优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28338760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com