8

我的代码无法使用 Visual Studio 2015 社区版进行编译,并出现以下错误:

致命错误 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;
}

这是我能够通过反复试验来归结失败状态的最简单的方法,但仍有很多事情要做。

让我目瞪口呆的是,以下每一个更改都会导致它编译得很好......

constexpr从的构造函数中删除B使其工作:

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;
}

A将's 变量更改为 notstatic使其工作:

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;
}

intB'sunion的第二个中使用纯文本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;
}

x在的构造函数中进行默认初始化B而不是传递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;
}

最后,将B'sunionInt成员从结构中取出使其工作:

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;
}

可以说,我完全不知所措。我将非常感谢比我更了解编译器的人的任何帮助。

4

0 回答 0