不完全类型的静态字段 - 是否合法? [英] Static field of an incomplete type - is it legal?
问题描述
在C ++中类定义合法的时候,声明一个类型的静态字段是不完整的吗?例如:
Is declaring a static field of a type that is incomplete at the moment of the class definition legal in C++? For example:
Foo.h:
class Foo
{
public:
// ...
private:
class Bar;
static Bar something;
};
Foo.cpp:
class Foo::Bar
{
// ...
};
Foo::Bar Foo::something;
// some more code
编译器似乎接受它,没有看到应该拒绝的理由,但标准对这种情况说了什么?我真的很感谢有人给出了标准的直接引用,最好是从旧的(从2003年开始)。
Compilers seem to accept it, and I don't see a reason for which it should be rejected, but what does the standard say about such situation? I'd really appreciate someone giving a direct quote from the standard, preferably from the "old" one (the one from 2003).
推荐答案
是的,你所做的是特别允许的(§9.4.2/ 2):静态数据成员在其类定义中的声明不是一个定义,可能是一个不完整的类型,而不是cv-qualified void。
Yes, what you're doing is specifically allowed (§9.4.2/2): "The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void."
这篇关于不完全类型的静态字段 - 是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!