【发布时间】:2017-12-21 07:08:27
【问题描述】:
我有一些行为非常奇怪的代码导致编译器崩溃。我正在使用 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; 行并从构造函数调用中删除相应的参数,错误就会消失。
如果我将main 中的recipes 变量更改为一个recipe,错误就会消失(就像Recipe a{ { { 0, 1 },{ 1, 2 } },{ 2, 1 } }; 一样)。
注意:
_Recipe 结构不能是匿名结构,因为它有一个std::vector<GoodRatio> 类型的数据成员,而Recipe::GoodRatio 类型只存在于typedef 之后的分号处。至少这是我的猜测。如果我尝试这样做,我会收到错误消息。
编辑:我的问题是:为什么会这样?
【问题讨论】:
-
那么你的问题是什么?内部编译器错误与编译器崩溃不同,但仍然值得向 Microsoft 报告。
-
如果编译器崩溃,那么这是编译器中的一个错误,无论您传递给它的代码如何。编译器永远不应该崩溃。请向您的编译器供应商报告该错误,以便他们修复它。
-
“为什么会这样?”因为编译器有bug。
-
@aschepler tnx。没有意识到这一点。我更改了标题并添加了我的问题。我以前从未有过这些,所以我不知道这只是意味着它是由编译器中的错误引起的。我会报告给 MS
-
我想提一下,您的
typedef structdecarations 并不是真正必要的。这样的构造在 C++ 中并不常见,也许它们是编译器错误的原因。尝试用struct Recipe{之类的简单声明替换它们。
标签: c++ visual-c++