类声明使该类成为不完整类型,因此无法定义该类的任何对象。不完整类型的定义是未指定其成员。这是否意味着一个具有空主体且在其定义中未指定成员的类会使其成为不完整类型?
class Empty { };
Empty e1; // okay, but why?
最佳答案
Is a class with empty body an incomplete type?
不,具有空主体的类只是一个空类,但仍然是一个完全定义的类。不完整类型是指其完整定义不可见的类型。
class Empty;
// Here, Empty is an incomplete type
class Empty { };
// Here, Empty is a complete type
根据 C++11 标准第 3.9/5 段:
A class that has been declared but not defined, or an array of unknown size or of incomplete element type, is an incompletely-defined object type. Incompletely-defined object types and the void types are incomplete types (3.9.1). Objects shall not be defined to have an incomplete type.
此外,根据第 9.2/2 段:
A class is considered a completely-defined object type (3.9) (or complete type) at the closing
}
of the class-specifier. [...]
关于c++ - 主体为空的类是不完整类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16573191/