This code is giving me incomplete type error. What is the problem? Isn't allowed for a class to have static member instances of itself? Is there a way to achieve the same result?
struct Size
{
const unsigned int width;
const unsigned int height;
static constexpr Size big = { 480, 240 };
static constexpr Size small = { 210, 170 };
private:
Size( ) = default;
};
As a workaround you can use a separate base class which definition is complete when defining the constants in the derived class.
You can put the base class in a separate namespace to hide its definition if you want to.
The code compiles with clang and gcc
By "the same result", do you specifically intend the
constexpr
-ness ofSize::big
andSize::small
? In that case maybe this would be close enough:A class is allowed to have a static member of the same type. However, a class is incomplete until the end of its definition, and an object cannot be defined with incomplete type. You can declare an object with incomplete type, and define it later where it is complete (outside the class).
see this here: http://coliru.stacked-crooked.com/a/f43395e5d08a3952
This doesn't work for
constexpr
members, however.