The Standard seems to support (the snippet below compiles) a static data member having the same type as the class itself [duplicate]

StackOverflow https://stackoverflow.com/questions/22535154

문제

Where specifically is this covered in the Standard? I found §9.2/10: Non-static (9.4) data members shall not have incomplete types. In particular, a class C shall not contain a non-static member of class C, but it can contain a pointer or reference to an object of class C., but this doesn't seem to directly support the issue at hand.

#include <iostream>
struct A{
    int i;
    static A a;
};

A A::a{10};

int main() {
    std::cout << A::a.i << '\n';
}
도움이 되었습니까?

해결책

C++11 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top