引用不完全类型 [英] References to incomplete types
问题描述
根据C ++ 03标准,是否有引用不完全类型的有效性?我不知道任何实现引用作为任何非空指针,因此这样的代码应该工作。但是,我不知道对不完全类型的引用是否符合标准。
According to 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.
我会感谢带有引用和C ++标准引用的答案。
I would appreciate answers with quotes and references to the C++ standard.
推荐答案
根据我所知,C ++标准没有明确说明你可以引用一个不完整的类型。但标准中有一个部分指定了必须完成的时间:
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:
定义规则[basic.def.odr]
4。翻译中需要一个类的定义
unit如果类的使用方式要求类类型为
完成。 [示例:下面的完整翻译单元是
格式正确,即使它从未定义 X
:
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 >注意:声明和表达式的规则
描述了在哪些上下文中需要完整的类类型。类别
T
必须完成,如果:
—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:
-
T
类型定义(3.1,5.3.4)或 - 将左值转换为右值转换应用于左值引用
类型T
(4.1)的对象,或 - 一个表达式被转换(隐式或显式)到
类型T
(第4,5.2.3,5.2.7,5.2.9,5.4)或 - 一个不是空指针常量的表达式,并且具有类型
而不是void *
的表达式转换为T
或引用
到T
使用隐式转换(第4节),dynamic_cast
(5.2.7)
或astatic_cast
(5.2.9)或 - 应用于类型
T
(5.2.5)或 - 的表达式
typeid
运算符(5.2.8)或
sizeof
运算符(5.3.3)是
应用于T
或 - 定义具有
T
类型的返回类型或参数类型的函数
(3.1)或调用(5.2.2)或 - 分配类型
T
的左值)。 ]
- 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.
这篇关于引用不完全类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!