简体   繁体   English

引用不完整类型

[英]References to incomplete types

According to the C++03 standard, is it valid to have references to incomplete types? 根据C ++ 03标准,引用不完整类型是否有效? 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. 我很感激引用答案和引用C ++标准。

The C++ standard doesn't explicitly say, as far as I know, that you can have a reference to an incomplete type. 据我所知,C ++标准没有明确说明你可以引用一个不完整的类型。 But the standard does have a section that specifies when a type must be complete: 但该标准确实有一个部分,指定何时必须完成一个类型:

3.2 One definition rule [basic.def.odr] 3.2一个定义规则[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. 4.如果以需要类类型完整的方式使用类,则在翻译单元中需要一个类的正确定义。 [ Example: the following complete translation unit is well-formed, even though it never defines X : [ 示例:以下完整的翻译单元格式正确,即使它从未定义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. -end example ] [ 注意:声明和表达式的规则描述了在哪些上下文中需要完整的类类型。 A class type T must be complete if: 在以下情况下,类类型T必须完整:

  • an object of type T is defined (3.1, 5.3.4), or 定义了类型为T的对象(3.1,5.3.4),或
  • an lvalue-to-rvalue conversion is applied to an lvalue referring to an object of type T (4.1), or 左值到左值的转换应用于引用类型为T (4.1)的对象的左值,或者
  • 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 表达式(隐式或显式)转换为类型T (第4,5.2.3,5.2.7,5.2.9,5.4),或者
  • an expression that is not a null pointer constant, and has type other than void * is converted to the type pointer to T or reference to T using an implicit conversion (clause 4), a dynamic_cast (5.2.7) or a static_cast (5.2.9), or 表达式不是空指针常量,并且具有void *以外的类型,使用隐式转换(第4节), dynamic_cast (5.2.7)或static_cast (5.2)转换为T的类型指针或引用T .9),或
  • a class member access operator is applied to an expression of type T (5.2.5), or 类成员访问运算符应用于类型为T (5.2.5)的表达式,或
  • the typeid operator (5.2.8) or the sizeof operator (5.3.3) is applied to an operand of type T , or typeid运算符(5.2.8)或sizeof运算符(5.3.3)应用于类型T的操作数,或
  • a function with a return type or argument type of type T is defined (3.1) or called (5.2.2), or 返回类型或类型为T参数类型的函数定义为(3.1)或调用(5.2.2),或
  • an lvalue of type T is assigned to (5.17). 类型T的左值被赋予(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 §15.1.3说

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*. throw-expression的类型不应是不完整类型,也不应是指向不完整类型的指针或引用,而不是void *,const void *,volatile void *或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. 我还不太擅长解释标准,所以请按照你的意愿行事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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