gpt4 book ai didi

C++ 强制转换运算符重载和多态性

转载 作者:IT老高 更新时间:2023-10-28 21:43:03 27 4
gpt4 key购买 nike

我对 C++ 的这种行为感到困惑:

struct A {
virtual void print() const { printf("a\n"); }
};

struct B : public A {
virtual void print() const { printf("b\n"); }
};

struct C {
operator B() { return B(); }
};

void print(const A& a) {
a.print();
}

int main() {
C c;
print(c);
}

那么,测验是,程序的输出是什么 - a 还是 b?嗯,答案是一个。但为什么呢?

最佳答案

这里的问题是 C++03 标准中的一个错误/错误功能/漏洞,不同的编译器试图以不同的方式修补该问题。 (这个问题在 C++11 标准中已经不存在了。)

这两个标准的第 8.5.3/5 节指定了如何初始化引用。这是 C++03 版本(列表编号是我的):

A reference to type cv1 T1 is initialized by an expression of type cv2 T2 as follows:

  1. If the initializer expression

    1. is an lvalue (but is not a bit-field), and “cv1 T1” is reference-compatible with “cv2 T2,” or
    2. has a class type (i.e., T2 is a class type) and can be implicitly converted to an lvalue of type cv3 T3, where cv1 T1 is reference-compatible with cv3 T3

    then the reference is bound directly to the initializer expression lvalue in the first case, and the reference is bound to the lvalue result of the conversion in the second case.

  2. Otherwise, the reference shall be to a non-volatile const type (i.e., cv1 shall be const).

  3. If the initializer expression is an rvalue, with T2 a class type, and cv1 T1 is reference-compatible with cv2 T2, the reference is bound in one of the following ways (the choice is implementation-defined):

    1. The reference is bound to the object represented by the rvalue (see 3.10) or to a sub-object within that object.
    2. A temporary of type cv1 T2 [sic] is created, and a constructor is called to copy the entire rvalue object into the temporary. The reference is bound to the temporary or to a sub-object within the temporary.

    The constructor that would be used to make the copy shall be callable whether or not the copy is actually done.

  4. Otherwise, a temporary of type cv1 T1 is created and initialized from the initializer expression using the rules for a non-reference copy initialization (8.5). The reference is then bound to the temporary.

手头的问题涉及三种类型:

  • 要创建的引用的类型。标准(两个版本)将此类型表示为 T1。在这种情况下,它是 struct A
  • 初始化表达式的类型。标准将此类型表示为 T2。在这种情况下,初始化表达式是变量 c,所以 T2struct C。请注意,由于 struct Astruct C 不是 reference-compatible,因此无法直接将引用绑定(bind)到 c。需要一个中间人。
  • 中间体的类型。标准将此类型表示为 T3。在这种情况下,这是 struct B。请注意,将转换运算符 C::operator B() 应用于 c 会将左值 c 转换为右值。

我标记为 1.1 和 3 的初始化已经结束,因为 struct Astruct C 的引用不兼容。需要使用转换运算符C::operator B()。 1.2 out 因为这个转换运算符返回一个右值,所以这个规则 1.2 out。剩下的就是选项 4,创建一个 cv1 T1 类型的临时文件。严格遵守 2003 版标准会强制为这个问题创建两个临时文件,即使只有一个就足够了。

2011 版标准通过将选项 3 替换为

解决了该问题
  • If the initializer expression

    • is an xvalue, 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 implicitly converted to an xvalue, class prvalue, or function lvalue of type cv3 T3, where cv1 T1 is reference-compatible with cv3 T3,

    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). In the second case, if the reference is an rvalue reference and the second standard con- version sequence of the user-defined conversion sequence includes an lvalue-to-rvalue conversion, the program is ill-formed.

似乎 gcc 系列编译器选择了严格遵从而不是意图(避免创建不必要的临时变量),而其他打印“b”的编译器选择了对标准的意图/更正。选择严格合规不一定值得称道。在 2003 版标准(例如 std::set)中还有其他错误/错误功能,其中 gcc 家族选择理智而不是严格遵守。

关于C++ 强制转换运算符重载和多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14298715/

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