c++ - VC++ 17 内部编译器错误

标签 c++ visual-c++

我有一些非常奇怪的行为代码导致编译器崩溃。我正在使用 VC++ 17:

//.hpp
typedef unsigned short UID;
typedef UID GoodType;
typedef struct _Recipe
{
    typedef struct {
        GoodType goodType; unsigned short units;
    } GoodRatio;
    std::vector<GoodRatio> input;
    GoodRatio output;
    //int a;
} Recipe;

//.cpp
int main()
{
    std::vector<Recipe> recipes
    {
        {
            { { 0, 1 }, { 1, 2 } }, { 2, 1 }
        },
    };
}

错误:

1>c:\users\peter\downloads\nationsgamemockup\nationsgamemockup\nationsgamemockup.cpp(25): fatal error C1001: An internal error has occurred in the compiler.
1>(compiler file 'f:\dd\vctools\compiler\utc\src\p2\main.c', line 255)
1> To work around this problem, try simplifying or changing the program near the locations listed above.

到目前为止我观察到的行为:

如果我取消注释 //int a;无论我是否在主函数的构造函数调用中为 a 添加参数,错误都会消失。

如果我评论 std::vector<GoodRatio> input;行并从构造函数调用中删除相应的参数,错误消失。

如果我评论 GoodRatio output;行并从构造函数调用中删除相应的参数,错误消失。

如果我更改 recipes main 中的变量到一个recipe错误消失了(就像简单的 Recipe a{ { { 0, 1 },{ 1, 2 } },{ 2, 1 } }; 一样)。

注意: _Recipe struct 不能是匿名结构,因为它有一个 std::vector<GoodRatio> 类型的数据成员和 Recipe::GoodRatio type 仅在 typedef 之后的分号处出现。至少这是我的猜测。如果我尝试这样做,我会收到错误消息。

编辑:我的问题是:为什么会这样?

最佳答案

向 GoodRatio 添加构造函数似乎可以解决问题:

GoodRatio(unsigned short goodType = {}, unsigned short units = {}) :
goodType{ goodType }, units{ units } {}

我也向微软发送了反馈。

关于c++ - VC++ 17 内部编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45132490/

相关文章:

c++ - 如何从 QString 中包含的十六进制值中获取 ASCII 字符?

c++ - 在 VIsual C++ 中使用 C++ 代码,没有错误,但部分代码被忽略

visual-studio - 将驱动程序项目从 VS2013 迁移到 VS2015 社区

c++ - 从其他专门函数调用专门函数

c++ - Visual C++ 警告 C4800,为什么它只在 return 语句上触发?

c++ - 从另一个 vector 搜索和查找一个 vector 中的元素的有效方法?

C++:不能 static_cast 从 double* 到 int*

c# - 如何将函数从 C++ .dll 导入到 C Sharp

c++ - NotImplementedException C++

c++ - 使用 macports 构建 boost 后找不到 .a 静态库文件,我只有 dylib