According t开发者_如何学编程o the C++03 standard, is it valid to have references to incomplete types? I'm not aware of any implementation that implements references as any other than non-null pointers, so such code ought to work. However, I wonder whether references to incomplete types are standard conforming.
I would appreciate answers with quotes and references to the C++ standard.
The C++ standard doesn't explicitly say, as far as I know, that you can have a reference to an incomplete type. But the standard does have a section that specifies when a type must be complete:
3.2 One definition rule [basic.def.odr]
4. Exactly one definition of a class is required in a translation unit if the class is used in a way that requires the class type to be complete. [Example: the following complete translation unit is well-formed, even though it never defines
X
:struct X; // declare X as a struct type struct X* x1; // use X in pointer formation X* x2; // use X in pointer formation
—end example] [Note: the rules for declarations and expressions describe in which contexts complete class types are required. A class type
T
must be complete if:
- an object of type
T
is defined (3.1, 5.3.4), or- an lvalue-to-rvalue conversion is applied to an lvalue referring to an object of type
T
(4.1), or- an expression is converted (either implicitly or explicitly) to type
T
(clause 4, 5.2.3, 5.2.7, 5.2.9, 5.4), or- an expression that is not a null pointer constant, and has type other than
void *
is converted to the type pointer toT
or reference toT
using an implicit conversion (clause 4), adynamic_cast
(5.2.7) or astatic_cast
(5.2.9), or- a class member access operator is applied to an expression of type
T
(5.2.5), or- the
typeid
operator (5.2.8) or thesizeof
operator (5.3.3) is applied to an operand of typeT
, or- a function with a return type or argument type of type
T
is defined (3.1) or called (5.2.2), or- an lvalue of type
T
is assigned to (5.17). ]
It appears that in every other case, including declarations of references to incomplete types, the type can be incomplete.
§15.1.3 says
The type of the throw-expression shall not be an incomplete type, or a pointer or reference to an incomplete type, other than void*, const void*, volatile void*, or const volatile void*.
Which seems to imply that you can have a reference to an incomplete type.
I'm not quite good at interpreting the standard yet, so take that how you will.
精彩评论