static constexpr member of same type as class being defined
If I interpret the Standard correctly, it isn't possible.
(§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. [...]
From the above (along with the fact that there is no separate statement about non-literal types in static data member declarations), I believe it follows that a static data member that is constexpr
must be a literal type (as defined in §3.9/10), and it must have its definition included in the declaration. The latter condition could be satisfied by using the following code:
struct Foo {
constexpr Foo() {}
static constexpr Foo f {};
};
which is similar to your Attempt 1, but without the class-external definition.
However, since Foo
is incomplete at the time of declaration/definition of the static member, the compiler can't check whether it is a literal type (as defined in §3.9/10), so it rejects the code.
Note that there is this post-C++-11 document (N3308) which discusses various problems of the current definition of constexpr
in the Standard, and makes suggestions for amendments. Specifically, the "Proposed Wording" section suggests an amendment of §3.9/10 that implies the inclusion of incomplete types as one kind of literal type. If that amendment was to be accepted into a future version of the Standard, your problem would be solved.
I believe GCC is incorrect to reject your Attempt 3. There is no rule in the C++11 standard (or any of its accepted defect reports) which says that a redeclaration of a variable must be constexpr
iff the prior declaration was. The closest the standard comes to that rule is in [dcl.constexpr](7.1.5)/1_:
If any declaration of a function or function template has
constexpr
specifier, then all its declarations shall contain theconstexpr
specifier.
Clang's implementation of constexpr
accepts your Attempt 3.