AndyG AndyG - 1 month ago 8
C++ Question

Static templated constexpr nested class member

I have the following sample class

Foo
with nested class
Bar
and everything is
constexpr
:

class Foo
{
private:
template <typename T>
struct Bar
{
constexpr Bar(){}
constexpr int DoTheThing() const
{
return 1;
}
};

public:
constexpr static auto b = Bar<int>{};
constexpr Foo() {}
constexpr int DoTheThing() const
{
return b.DoTheThing();
}
};


And I want to test that calling
Foo::DoTheThing
returns 1:

int main()
{
constexpr Foo f;
static_assert(f.DoTheThing() == 1, "DoTheThing() should return 1");
}


GCC and Clang both complain here, but MSVC does not

GCC says:


error:
constexpr Foo::Bar<T>::Bar() [with T = int]
used before its definition

constexpr static auto b = Bar<int>{};



And Clang:


error: constexpr variable
b
must be initialized by a constant expression

constexpr static auto b = Bar<int>{};



I cannot tell if the standard disallows this, but my guess is that somehow
b
is an incomplete type.

What makes things more interesting is that I can get GCC and Clang to behave if I remove the
constexpr
, or if I move the definition of
Bar
outside of
Foo
.

Which of these compilers is correct?



Note that this question was inspired by the following:


Answer

From n4140

ยง 9.2.2 [class.mem] (Emphasis mine)

A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments, using-declarations introducing inheriting constructors (12.9), exception-specifications, and brace-or-equal-initializers for non-static data members (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification.

Clang and GCC are correct. The class is not considered complete when you are declaring your static constexpr member, so you cannot construct it. This is why moving the definition of Bar out or removing the static constexpr works (because it is considered complete when defining non-static members)