References to incomplete types

匆匆过客 提交于 2019-12-22 09:32:30

问题


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.

I would appreciate answers with quotes and references to the C++ standard.


回答1:


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 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
  • a class member access operator is applied to an expression of type T (5.2.5), or
  • the typeid operator (5.2.8) or the sizeof operator (5.3.3) is applied to an operand of type T, 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.




回答2:


§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.



来源:https://stackoverflow.com/questions/7356603/references-to-incomplete-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!