这应该是模糊还是不? (隐式转换)

编程入门 行业动态 更新时间:2024-10-21 12:56:08
本文介绍了这应该是模糊还是不? (隐式转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

struct A { A(const A& src); A(const char * src); }; struct B { operator A(); operator char *(); }; void test() { B v; A s(v); }

EDG / Comeau和MSVC允许代码,而GCC 4.4.4,

一个C ++委员会成员回答这个问题(最初):

这不明确; A(const A&)构造函数比A(const char *)构造函数更好。 const A& 参数直接绑定到转换函数的结果,因此转换序列被认为是是用户定义的转换,后跟通过标识转换(13.3.3.1.4p1)。 const char * 参数是一个用户定义的转换,后面是一个限定条件的转换,因此更糟。

然后,他跟着这个。

其实我错了。虽然它是真的,用户定义的转换序列中的第二个转换序列是一个tiebreaker,看起来更多的密切在13.3.3.2p3, next-to-last bullet,显示这个 tiebreaker仅适用于两个序列包含相同的用户定义的转换序列,而不是这种情况例。 因为一个构造函数的转换序列使用B :: operator A()和其他使用b :: operator char *(),两个$ b之间没有tiebreak $ b用户定义的转换序列和他们是不明确的。

我的问题是这个。 $ b

13.3.3.2 p3说明

的两个隐式转换序列

根据我的理解,关键字是关键字,它们是不可区分的转换序列,除非遵循规则之一。 以下规则之一。 这并不意味着说明相同的转换序列的项目符号覆盖所有上面的。我认为S1的排名比S2的排名更好b b b

解决方案

是,预期结果是我对条款13.3.3.2的最佳解释的Ambiguity

将类型'B'的参数'v'的'A'的重载构造函数需要用户定义的转换。这两个序列都是CONVERSION排名。

我的解释是,$ 13.3.3.2以下的引用适用

...]用户定义的转换序列 U1是一个更好的转换序列比另一个用户定义的转换序列U2 如果它们包含相同的用户定义转换函数或构造函数和,如果U1的第二标准转换序列比U2的第二标准转换序列更好的。

这两个类都在类'B'中调用不同的转换函数。因此,我认为第一个条件本身不满足,因此预期的结果是歧义,因为任何一个转换序列比另一个更好。

struct A { A(const A& src); A(const char* src); }; struct B { operator A(); operator char*(); }; void test() { B v; A s(v); }

EDG/Comeau and MSVC allows the code while GCC 4.4.4, CLANG and BCC reject it as ambiguous.

A C++ committee member replied with this (initially):

It's not ambiguous; the A(const A&) constructor is better than the A(const char*) constructor. The const A& parameter binds directly to the result of the conversion function, so the conversion sequence is considered to be a user-defined conversion followed by an identity conversion (13.3.3.1.4p1). The const char* parameter is a user-defined conversion followed by a qualification conversion, so it's worse.

Then, he followed up with this.

Actually, I was wrong. While it is true that the second conversion sequence in a user-defined conversion sequence is a tiebreaker, looking more closely at 13.3.3.2p3, the next-to-last bullet, reveals that this tiebreaker only applies if the two sequences contain the same user-defined conversion sequence, and that is not the case in this example. Because one constructor's conversion sequence uses B::operator A() and the other uses b::operator char*(), there's no tiebreaker between the two user-defined conversion sequences and they are ambiguous.

My question is this.

13.3.3.2 p3 states that,

Two implicit conversion sequences of the same form are indistinguishable conversion sequences unless one of the following rules apply.

From my understanding, the keywords are "one of the following rules". It doesn't mean that the bullet which states "same conversion sequence" overrides all the ones above. I would've thought "the rank of S1 is better than the rank of S2" would be applicable instead?

解决方案

Yes, the expected result is Ambiguity to the best of my interpretation of Clause 13.3.3.2

Matching argument 'v' of type 'B' to the parameters of either of the overloaded constructors of 'A' requires a user defined conversion. There both the sequences are of CONVERSION rank.

My interpretation is that the following quote from $13.3.3.2 applies

[...]User-defined conversion sequence U1 is a better conversion sequence than another user-defined conversion sequence U2 if they contain the same user-defined conversion function or constructor and if the second standard conversion sequence of U1 is better than the second standard conversion sequence of U2.

Both of these invoke different conversion functions in class 'B'. Therefore, I think the first condition itself is not satisfied and hence the expected result is Ambiguity as nither of the conversion sequences is better than the other.

更多推荐

这应该是模糊还是不? (隐式转换)

本文发布于:2023-11-15 21:25:36,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模糊   隐式   这应

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!