gpt4 book ai didi

c++ - (GCC 错误?)隐式转换为派生类

转载 作者:IT老高 更新时间:2023-10-28 22:14:55 26 4
gpt4 key购买 nike

我在 C++ 中遇到了隐式转换问题。下面是一个最小的例子:

struct A {
virtual void f()=0; // abstract
};

struct Ad : A {
virtual void f() {} // not abstract
};

struct B {
operator Ad () const { return Ad(); }
};

void test(A const &lhs) {}

int main()
{
B b;
test(b);
}

我希望编译器做的是:将 b 转换为 Ad 类型的变量(使用 B 中定义的转换)并将结果传递给 test。但是,上述代码在 GCC(启用 C++11)中无法编译,结果是无法分配抽象类型“A”的对象

注意事项:

  1. Clang 编译这个。
  2. 如果您通过将 f()=0; 更改为 f() {} 来使 A 成为非抽象,则代码可以正常工作。
  3. 编译器确实找到了转换运算符(如 2 所示),但它没有做我想做的事情。

最佳答案

(所有引述来自 N4140,C++14 FD)

TL;DR:代码格式正确,这是(或曾经是)GCC 错误。

引用初始化的规则在 [dcl.init.ref]/5 中介绍。我将首先向您展示未涵盖它的项目符号 - 如果您想跳过它,请直接进入第三个引号。

Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be const), or the reference shall be an rvalue reference.

  • If the initializer expression
    • is an xvalue (but not a bit-field), class prvalue, array prvalue or function lvalue and “cv1 T1” is reference-compatible with “cv2 T2”, or
    • has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be converted to an xvalue, class prvalue, or function lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3” (see 13.3.1.6),

then the reference is bound to the value of the initializer expression in the first case and to the result of the conversion in the second case (or, in either case, to an appropriate base class subobject).

并且引用兼容性在 [dcl.init.ref]/41 中定义。
现在考虑链接的 13.3.1.6:

Under the conditions specified in 8.5.3, a reference can be bound directly to a glvalue or class prvalue that is the result of applying a conversion function to an initializer expression. Overload resolution is used to select the conversion function to be invoked. Assuming that “cv1 T” is the underlying type of the reference being initialized, and “cv S” is the type of the initializer expression, with S a class type, the candidate functions are selected as follows:

  • The conversion functions of S and its base classes are considered. Those non-explicit conversion functions that are not hidden within S and yield type “lvalue reference to cv2 T2 (when initializing an lvalue reference or an rvalue reference to function) or “cv2 T2” [..], where “cv1 T” is reference-compatible (8.5.3) with “cv2 T2”, are candidate functions. For direct-initialization, [..].

如您所见,您的转换函数不是本段之后的候选函数。因此 [dcl.init]/5 中的下一个项目符号适用:

Otherwise:

  • If T1 is a class type, user-defined conversions are considered using the rules for copy-initialization of an object of type “cv1 T1” by user-defined conversion (8.5, 13.3.1.4); the program is ill-formed if the corresponding non-reference copy-initialization would be ill-formed. The result of the call to the conversion function, as described for the non-reference copy-initialization, is then used to direct-initialize the reference. The program is ill-formed if the direct-initialization does not result in a direct binding or if it involves a user-defined conversion.

请注意,“程序是 如果相应的非引用复制初始化,则格式错误 将是不正确的”可能意味着

B b;
A a = b;

格式不正确,程序格式不正确。我认为这是措辞上的缺陷或含糊不清,而不是 GCC 不接受代码的原因。可以肯定的是,该措辞仅针对初始化本身,而不是可以首先创建类型为 T1(又名 A)的最衍生对象。
最后 13.3.1.4 接受我们的转换函数:

Assuming that “cv1 T” is the type of the object being initialized, with T a class type, the candidate functions are selected as follows:

  • The converting constructors (12.3.1) of T are candidate functions.
  • When the type of the initializer expression is a class type “cv S”, the non-explicit conversion functions of S and its base classes are considered. [..]. Those that are not hidden within S and yield a type whose cv-unqualified version is the same type as T or is a derived class thereof are candidate functions.

现在最后一个问题是是否在

A const& ref(Ad());

ref 直接绑定(bind)。确实如此。
因此,原始参数引用直接绑定(bind),并且不必创建类型为 A 的最派生对象。
GCC 可能认为 A 类型的临时文件必须被初始化,并且引用必须绑定(bind)到该临时文件。或者它迂腐地遵循上述有缺陷的措辞,这不太可能。


1)

Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is reference-related to “cv2 T2” if T1 is the same type as T2, or T1 is a base class of T2. “cv1 T1” is reference-compatible with “cv2 T2” if T1 is reference-related to T2 and cv1 is the same cv-qualification as, or greater cv-qualification than, cv2.

关于c++ - (GCC 错误?)隐式转换为派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27675893/

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