问题描述
当使用VC12(在Visual Studio 2013 RTM中)编译时,此程序导致崩溃(在所有版本配置中),但实际上不应该:
This program, when compiled with VC12 (in Visual Studio 2013 RTM) leads to a crash (in all build configurations), when really it shouldn't:
#include <string>
void foo(std::string const& oops = {})
{
}
int main()
{
foo();
}
我知道两个沉默的错误codegen错误 有关:
I know of two silent bad codegen bugs that might be related:
- https://connect.microsoft.com/VisualStudio/feedback/details/800364/initializer-list-calls-object-destructor-twice
- http://connect.microsoft.com/VisualStudio/feedback/details/800104/
老实说,我认为这些不同。是否有人知道
Honestly I think these are different, though. Does anyone know
- 是否有
- 一个解决方法(或者引起这个bug的情况的一个明确的描述,所以我们可以在代码库中查找/避免它)。
只需使用C ++控制台应用程序向导创建一个空项目。为了简单起见,请停用预先编译的标题,并保留所有默认值:
Just create an empty project using the C++ Console Application 'wizard'. For simplicity, disable precompiled headers and leave all defaults: http://i.stack.imgur.com/rrrnV.png
推荐答案
活动问题已发布在。发布的示例代码为:
An active issue was posted back in November. The sample code posted was:
Compile and run following code in VS2013
#include <string>
void f(std::string s = {}) {
}
int main(int argc, char* argv[]) {
f();
return 0;
}
该错误已被Microsoft承认。
The bug has been acknowledged by Microsoft.
这里似乎没有一个工作。 修改解决方法很容易避免使用list-initializer语法:
There doesn't seem to be a work-around posted there. Edit Workarounds can easily be based on avoiding the list-initializer syntax:
void f(std::string s = "");
void f(std::string s = std::string());
void f(std::string s = std::string {});
或者只是老式的(如果你不介意引入重载):
Or just the old-fashioned (if you don't mind introducing overloads):
void f(std::string s);
void f() { f(std::string()); }
这篇关于(已知)VC12中的编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!