gpt4 book ai didi

c++ - C++中的转换优先级

转载 作者:行者123 更新时间:2023-12-02 10:26:29 24 4
gpt4 key购买 nike

我有以下代码:

Some functions:

A::A(int i_a) {cout<<"int Ctor\n";} //conversion constructor

void h(double d) {cout<<"double param\n";} //f1
void h(A a) {cout<<"A param\n";} //f2

在主要功能中:
h(1);

h(1)调用的函数为f1。

我的问题是,为什么选择调用它。 1是一个整数,因此需要
隐式转换为double。它可以很容易地将int转换为A
上面定义的转换构造函数。为什么我没有收到错误消息?什么是
优先规则与强制转换?

N.b.我在代码上方贴了我认为有必要回答该问题的代码,
但下面我发布了整个代码:
 #include <iostream>
using namespace std;
class B;
class A {
public:
explicit A(const B&) {cout<<"Ctor through B\n";}
A() {cout<<"Default Ctor\n";}
A(int i_a) {cout<<"int Ctor\n";}
operator int() {cout<<"A => int\n"; return 2;}
};
class B {
public:
operator A() const {cout<<"B => A\n"; A a; return a;}
};
void h(double d) {cout<<"double param\n";}
void h(A a) {cout<<"A param\n";}
void f(const A& a)
{
cout<<"f function\n";
//Without the const it will never try to convert
}
void main()
{
B b;
cout <<"-----------------\n";
f(b);
cout <<"-----------------\n";
h(1);
}

最佳答案

当执行重载解析以从所有可行的重载中选择最佳候选者时,编译器将为每个候选者的每个参数排列所有转换序列。为了使一个函数获胜(被选为最佳候选者),其每个参数的转换等级必须大于或等于该参数的其他函数的转换等级,并且至少一个转换等级必须优于所有其他函数函数的转换针对特定参数排名。
用户定义的转换(使用构造函数或强制转换运算符)具有可能的最差排名之一(仅省略号的排名更差)。整数浮点转换的排名更好(请参阅下面的排名列表)。
因此,编译器比转换int-> A(使用用户定义的转换)更喜欢转换int-> double(使用标准转换),因此选择f1。
编辑:尽管“过载解决方案”在后台工作,并且大多数时间确实符合您的期望(即,大多数程序员不需要深入研究技术性知识)-如果您确实想更深入(但请注意,有些重载解析的较暗角被认为是编译器编写者在C++编译器中正确使用的最棘手的方面之一),请参阅出色的C++ Templates: The Complete Guide by David Vandevoorde and Nicolai M. Josuttis,它在附录B中提供了对重载背后机制的最佳介绍之一。我读过的分辨率。
这是B.2的摘录:

Overload resolution ranks the viablecandidate functions by comparing howeach argument of the call matches thecorresponding parameter of thecandidates. For one candidate to beconsidered better than another, thebetter candidate cannot have any ofits parameters be a worse match thanthe corresponding parameter in theother candidate....

Given this first principle, we areleft with specifying how well a givenargument matches the correspondingparameter of a viable candidate. As afirst approximation we can rank thepossible matches as follows (from bestto worst):

Perfect match. The parameter has thetype of the expression, or it has atype that is a reference to the typeof the expression (possibly with addedconst and/or volatile qualifiers).

Match with minor adjustments. Thisincludes, for example, the decay of anarray variable to a pointer to itsfirst element, or the addition ofconst to match an argument of typeint** to a parameter of type intconst* const*.

Match with promotion. Promotion is akind of implicit conversion thatincludes the conversion of smallintegral types (such as bool, char,short, and sometimes enumerations) toint, unsigned int, long or unsignedlong, and the conversion of float todouble.

Match with standard conversions only.This includes any sort of standardconversion (such as int to float) butexcludes the implicit call to aconversion operator or a convertingconstructor.

Match with user-defined conversions.This allows any kind of implicitconversion.

Match with ellipsis. An ellipsisparameter can match almost any type(but non-POD class types result inundefined behavior).


但这仅仅是开始-如果您感兴趣-我敦促您阅读本书,然后阅读标准的相关部分:)

关于c++ - C++中的转换优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64275853/

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