C++ static constexpr field with incomplete type

Unfortunately, you simply cannot do this!

Some static constexpr members may be initialised inline:

[C++11 9.4.2/3]: [..] A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. [..]

Cursor is a literal type, so this counts.

And the use of Cursor itself as a static data member within its own type is not a problem, as long as you initialise it at lexical namespace scope:

[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. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. The initializer expression in the definition of a static data member is in the scope of its class (3.3.7).

But you can’t do that with constexpr:

[C++11: 7.1.5/9]: A constexpr specifier used in an object declaration declares the object as const. Such an object shall have literal type and shall be initialized. [..]

I think all of this wording could be improved but, in the meantime, I think you’re going to have to make ZERO a non-member in the enclosing namespace.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)