简体   繁体   中英

Isn't the word `polymorphic` missing in §1.3.7 (N4140)?

Isn't the word polymorphic (in parentheses) below, missing in §1.3.7 dynamic type ?

[ Example: if a pointer (8.3.1) p whose static type is “pointer to class B” is pointing to an object of a ( polymorphic ) class D, derived from B (Clause 10), the dynamic type of the expression *p is “D.” References (8.3.2) are treated similarly. —end example ]

According to the C++ Standard (10.3 Virtual functions)

1 Virtual functions support dynamic binding and object-oriented programming. A class that declares or inherits a virtual function is called a polymorphic class .

In the quote you cited there is not required that the classes would be polymorphic that is that they had a virtual function. So including this word would not be correct.

The quote gives a general definition of the dynamic type irrespective of whether the classes are polymorphic or are not..

I don't think the definition of "dynamic type" needs that qualification. If you look through the rest of the standard, you'll see that when the static and dynamic types are different, that doesn't automatically imply that the expression behaves like the dynamic type. This is specified to only happen when the static type is polymorphic. For example, see [expr.typeid]/2-3:

When typeid is applied to a glvalue expression whose type is a polymorphic class type (10.3), the result refers to a std::type_info object representing the type of the most derived object (1.8) (that is, the dynamic type) to which the glvalue refers. If the glvalue expression is obtained by applying the unary * operator to a pointer and the pointer is a null pointer value (4.10), the typeid expression throws an exception (15.1) of a type that would match a handler of type std::bad_typeid exception (18.7.3).

When typeid is applied to an expression other than a glvalue of a polymorphic class type, the result refers to a std::type_info object representing the static type of the expression. Lvalue-to-rvalue (4.1), array-to- pointer (4.2), and function-to-pointer (4.3) conversions are not applied to the expression. If the type of the expression is a class type, the class shall be completely-defined. The expression is an unevaluated operand (Clause 5).

In paragraph 3 you can see that it specifically says "static type". That's because the static and dynamic types can still differ, but only the static type matters because it's not polymorphic.

Firstly, even if we wanted to require polymorphism in this case, it would make more sense to impose that requirement on class B , not D (and as a consequence of that D would become polymophic as well).

Secondly, the general concept of dynamic type does not in any way rely on polymoprphism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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