struct Foo
{
inline static Foo F1;
};
This causes compilation error on various online compilers of C++11, C++17, C++20 due to ‘Foo Foo::F1’ has incomplete type
. In Visual Studio with C++17 it compiles and runs fine, but with the IntelliSense warning incomplete type is not allowed
. Why? Is there something potentially hazardous about doing this?
Inside the class definition of Foo
, Foo
itself is still incomplete (except in complete-class contexts).
inline
makes the static data member declaration a definition and in a definition of a variable its type must be complete.
However, you can add the inline
later. You just need to make sure that this external definition is present in every translation unit that includes the class definition (i.e. put it directly under the class in the header):
struct Foo
{
static Foo F1;
};
inline Foo Foo::F1;