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