gpt4 book ai didi

c++ - 对不完整类型的引用

转载 作者:太空狗 更新时间:2023-10-29 19:49:41 26 4
gpt4 key购买 nike

根据 C++03 标准,引用不完整类型是否有效?我不知道有任何实现将引用实现为非空指针以外的任何实现,所以这样的代码应该可以工作。但是,我想知道对不完整类型的引用是否符合标准。

如果您能提供引用和引用 C++ 标准的答案,我将不胜感激。

最佳答案

据我所知,C++ 标准没有明确说明您可以引用不完整的类型。但是该标准确实有一个部分指定类型必须是完整的:

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). ]

似乎在所有其他情况下,包括对不完整类型的引用的声明,类型可能是不完整的。

关于c++ - 对不完整类型的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7356603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com