编译器在第2遍中没有堆空间 - 匿名联合,匿名结构,constexpr构造函数,静态成员

Das*_*kie 6 c++ static unions constexpr anonymous-struct

我的代码无法使用Visual Studio 2015 Community Edition进行编译,但出现以下错误:

致命错误C1002:编译器在第2遍中没有堆空间

代码

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { Int y; };
    };

    constexpr B() : x(1) {}
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是最简单的我已经能够通过反复试验归结失败状态,但仍有相当多的事情发生.

让我目瞪口呆的是,以下每一项变化都会导致编译得很好......

constexprB构造函数中删除它使它工作:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { Int y; };
    };

    B() : x(1) {} // <---<<    ( constexpr B() : x(1) {} )
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

A变量变为static不起作用:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { Int y; };
    };

    constexpr B() : x(1) {}
};

struct A { B b; }; // <---<<    ( struct A { static B b; }; B A::b; )

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用普通intBunion的第二个struct而不是int包装使得它的工作:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { int y; }; // <---<<    ( struct { Int y; }; )
    };

    constexpr B() : x(1) {}
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

xB构造函数中进行默认初始化而不是传递1使其工作:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        struct { Int y; };
    };

    constexpr B() : x() {} // <---<<    ( constexpr B() : x(1) {} )
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

最后,采取BunionInt成员从结构使得它的工作:

struct Int { int i; };

struct B {
    union {
        struct { int x; };
        Int y; // <---<<    ( struct { Int y; }; )
    };

    constexpr B() : x(1) {}
};

struct A { static B b; };

B A::b;

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我只想说,我完全失去了.我非常感谢能比我更了解编译器的人提供的任何帮助.