简体   繁体   English

为什么以下带有非const转换函数的代码不含糊?

[英]Why the following code with non-const conversion function is not ambiguous?

Consider the following code (taken from https://en.cppreference.com/w/cpp/language/cast_operator ) 请考虑以下代码(摘自https://en.cppreference.com/w/cpp/language/cast_operator

struct To {
    To() = default;
    To(const struct From&) {} // converting constructor
};

struct From {
    operator To() const {return To();} // conversion function
};

int main()
{
    From f;

    To t2 = f; // copy-initialization: ambiguous
// (note, if conversion function is from a non-const type, e.g.
//  From::operator To();, it will be selected instead of the ctor in this case)
}

As the comments say, the following line is indeed ambiguous because there are two candidates (The conversion function and the converting constructor are equally applicable) 正如评论所说,以下行确实含糊不清,因为有两个候选者(转换函数和转换构造函数同样适用)

To t2 = f; //compile error

However, as the note says if I remove the const from the conversion function resulting in the following code: 但是,正如笔记所说,如果我从conversion函数中删除const ,导致以下代码:

struct From {
    operator To() {return To();} // conversion function
};

The call compiles fine. 电话编译很好。
The const qualifier should not affect the conversion function return value, so why the call is no longer ambiguous? const限定符不应该影响conversion函数的返回值,那么为什么调用不再含糊不清?

The const qualifier should not affect the conversion function return value, so why the call is no longer ambiguous? const限定符不应该影响转换函数的返回值,那么为什么调用不再含糊不清?

It does not affect the result, but it affects overload resolution for choosing the best viable method. 它不会影响结果,但它会影响重载分辨率以选择最佳可行方法。 It's similar to the case of these made up functions 它类似于这些组成功能的情况

To make(From const&);
To make(From&);

Which overload is the better match in make(f) ? 哪个重载是make(f)更好的匹配? It's the second one, because the parameter type, being non-const, better matches the argument ( f ), which is non-const itself. 这是第二个,因为参数类型是非const,更好地匹配参数( f ),它本身是非const。

The const qualifier should not affect the conversion function return value const限定符不应影响转换函数的返回值

It doesn't affect that indeed, but that's also not relevant. 它确实没有影响,但这也无关紧要。

What it does affect is the argument - which is the implicit reference to this . 它的真正影响是论点-这是隐式引用this The implicit argument is a const lvalue for const member functions and non-const for non-const member functions. 隐式参数是const成员函数的const值和非const成员函数的非const。 The arguments are what affect the overload resolution. 参数是影响重载决策的因素。

In the original code, both the constructor and the conversion operator arguments are exactly the same, so a conversion sequence from any type to either argument is equally preferable, and therefore ambiguous. 在原始代码中,构造函数和转换运算符参数完全相同,因此从任何类型到任一参数的转换序列同样可取,因此不明确。

Without the const, your non-const lvalue expression f doesn't need any conversions, while the constructor does require a conversion into const lvalue. 没有const,你的非const左值表达式f不需要任何转换,而构造函数确实需要转换为const左值。 As such, the operator is preferred by the overload resolution. 因此,过载分辨率优选操作员。 If you had written const From f; 如果你写了const From f; , then the constructor whose argument is const would have been chosen instead as in that case, the non-const conversion operator wouldn't even be valid candidate. ,然后选择其参数为const的构造函数,因为在这种情况下,非const转换运算符甚至不是有效的候选者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM