登 录
今天使用VS2005编译如下C++代码时,发生编译器Cl.exe crash:
测试代码:
static void f1 () { struct St { int x; std::string s; }; struct CompSt { bool operator () (const St &l, const St &r) { return l.x < r.x; } }; St datas[] = { {1, "1"}, {9, "9"}, {6, "6"}, {2, "2"}, {11, "11"}, {8, "8"}, {4, "4"}, }; int size = sizeof (datas) / sizeof (datas[0]); std::sort (datas, datas + size, CompSt()); for (int i = 0; i < size; ++i) { std::cout << datas[i].s << std::endl; } }
由于暂时无法上传图片,这里暂时不贴 cl.exe crash 的图片了。
不过上面代码如果改成这样:
struct St { int x; std::string s; }; static void f1 () { struct CompSt { bool operator () (const St &l, const St &r) { return l.x < r.x; } }; St datas[] = { {1, "1"}, {9, "9"}, {6, "6"}, {2, "2"}, {11, "11"}, {8, "8"}, {4, "4"}, }; int size = sizeof (datas) / sizeof (datas[0]); std::sort (datas, datas + size, CompSt()); for (int i = 0; i < size; ++i) { std::cout << datas[i].s << std::endl; } }
也就是把 St 的定义放到函数外面,编译通过,并能正确运行。
这是怎么回事?VS2005 的bug?
抱歉!评论已关闭.