gpt4 book ai didi

c++ - 符合标准的编译器可以拒绝包含来自非多态类型的 dynamic_cast downcast 的代码吗?

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

这个问题的灵感来自评论 here .

考虑以下代码片段:

struct X {}; // no virtual members
struct Y : X {}; // may or may not have virtual members, doesn't matter

Y* func(X* x) { return dynamic_cast<Y*>(x); }

一些人建议他们的编译器会拒绝 func 的正文.

但是,在我看来,这是否由标准定义取决于 x 的运行时值。 .来自第 5.2.7 节([expr.dynamic.cast]):

  1. The result of the expression dynamic_cast<T>(v) is the result of converting the expression v to type T. T shall be a pointer or reference to a complete class type, or "pointer to cv void." The dynamic_cast operator shall not cast away constness.

  2. If T is a pointer type, v shall be a prvalue of a pointer to complete class type, and the result is a prvalue of type T. If T is an lvalue reference type, v shall be an lvalue of a complete class type, and the result is an lvalue of the type referred to by T. If T is an rvalue reference type, v shall be an expression having a complete class type, and the result is an xvalue of the type referred to by T.

  3. If the type of v is the same as T, or it is the same as T except that the class object type in T is more cv-qualified than the class object type in v, the result is v (converted if necessary).

  4. If the value of v is a null pointer value in the pointer case, the result is the null pointer value of type T.

  5. If T is "pointer to cv1 B" and v has type 'pointer to cv2 D" such that B is a base class of D, the result is a pointer to the unique B subobject of the D object pointed to by v. Similarly, if T is "reference to cv1 B" and v has type cv2 D such that B is a base class of D, the result is the unique B subobject of the D object referred to by v. The result is an lvalue if T is an lvalue reference, or an xvalue if T is an rvalue reference. In both the pointer and reference cases, the program is ill-formed if cv2 has greater cv-qualification than cv1 or if B is an inaccessible or ambiguous base class of D.

  6. Otherwise, v shall be a pointer to or an lvalue of a polymorphic type.

  7. If T is "pointer to cv void," then the result is a pointer to the most derived object pointed to by v. Otherwise, a run-time check is applied to see if the object pointed or referred to by v can be converted to the type pointed or referred to by T.) The most derived object pointed or referred to by v can contain other B objects as base classes, but these are ignored.

  8. If C is the class type to which T points or refers, the run-time check logically executes as follows:

    • If, in the most derived object pointed (referred) to by v, v points (refers) to a public base class subobject of a C object, and if only one object of type C is derived from the subobject pointed (referred) to by v the result points (refers) to that C object.

    • Otherwise, if v points (refers) to a public base class subobject of the most derived object, and the type of the most derived object has a base class, of type C, that is unambiguous and public, the result points (refers) to the C subobject of the most derived object.

    • Otherwise, the run-time check fails.

  9. The value of a failed cast to pointer type is the null pointer value of the required result type. A failed cast to reference type throws std::bad_cast.

按照我的阅读方式,多态类型的要求仅在上述条件均不满足时适用,并且其中一个条件取决于运行时值。

当然,在少数情况下编译器可以肯定地确定输入不能正确地为 NULL(例如,当它是 this 指针时),但我仍然认为编译器不能拒绝代码,除非它可以确定声明将被达成(通常是运行时问题)。

警告诊断在这里当然很有值(value),但是编译器拒绝此代码并出现错误是否符合标准?

最佳答案

一个很好的观点。

注意C++03中5.2.7/3和5.2.7/4的写法如下

3 If the type of v is the same as the required result type (which, forconvenience, will be called R in this description), or it is the sameas R except that the class object type in R is more cv-qualified thanthe class object type in v, the result is v (converted if necessary).

4 If the value of v is a null pointer value in the pointer case, theresult is the null pointer value of type R.

对 5.2.7/3 中引入的类型 R 的引用似乎暗示 5.2.7/4 旨在成为 5.2.7/3 的子条款。换句话说,5.2.7/4 似乎只适用于 5.2.7/3 中描述的条件,即类型相同时。

但是,C++11 中的措辞不同,不再涉及R,不再暗示5.2.7/3 和5.2.7/4 之间有什么特殊关系。不知道是不是故意改的……

关于c++ - 符合标准的编译器可以拒绝包含来自非多态类型的 dynamic_cast downcast 的代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11301514/

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