静态模板化constexpr嵌套类成员

And*_*dyG 13 c++ templates language-lawyer constexpr c++14

我有以下Foo带嵌套类的示例类Bar,一切都是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();
    }
};
Run Code Online (Sandbox Code Playgroud)

我想测试那个调用Foo::DoTheThing返回1:

int main()
{
   constexpr Foo f;
   static_assert(f.DoTheThing() == 1, "DoTheThing() should return 1");
}
Run Code Online (Sandbox Code Playgroud)

GCC和Clang都在这里抱怨,但MSVC没有

GCC说:

错误:constexpr Foo::Bar<T>::Bar() [with T = int]在定义之前使用

constexpr static auto b = Bar<int>{};
Run Code Online (Sandbox Code Playgroud)

Clang:

错误:constexpr变量b必须由常量表达式初始化

constexpr static auto b = Bar<int>{};
Run Code Online (Sandbox Code Playgroud)

我不知道标准是否允许这样做,但我的猜测是,某种程度上b是不完整的类型.

让事情变得更有趣的是,如果我删除它constexpr,或者如果我移动Bar外部的定义,我可以让GCC和Clang表现出来Foo.

哪些编译器是正确的?

请注意,此问题的灵感来自以下方面:

And*_*dyG 5

从 n4140

§ 9.2.2 [class.mem](强调我的)

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)


To clarify, especially considering this question: Static constexpr member of an inner class

The standardese I quoted above basically means that unless otherwise specified a class is regarded incomplete within itself *. A static, constexpr, or static constexpr initializer does not fall under the otherwise specified portion, and therefore we can not use anything declared within the class, which includes a nested class type.

*meaning you can't use it or members of it within the class declaration. The most well known exception to that is within a member function.

  • 嗯...但是我怎么能做到这一点:`constexpr static auto b = sizeof(Bar&lt;int&gt;);` 你答案的第二部分基本上指出,外部类型的不完整性也意味着嵌套类型的不完整性(甚至如果后者“似乎”是完整的)。但是 `sizeof` 也需要一个 *complete* 类型作为它的参数。它是如何用这种嵌套类型编译的? (3认同)