我有以下示例代码:
#include <iostream>
using namespace std;
struct foo {
foo() { cout << "foo constructed.\n"; }
~foo() { cout << "foo destroyed.\n"; }
};
struct bar {
bar(foo t=foo{}) { }
};
int main(int argc, char **argv) {
bar X[2]{};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我用clang ++ -std = c ++ 11 test.cc编译它时,程序产生以下输出:
foo constructed.
foo constructed.
foo destroyed.
Run Code Online (Sandbox Code Playgroud)
但我预计还有一个"foo被摧毁".介于两个"foo构造"之间.线.为什么只有一个foo被摧毁?clang 3.5.1以及3.6.0会发生这种情况.
当使用VC12(在Visual Studio 2013 RTM中)[1]编译时,此程序会导致崩溃(在所有构建配置中),而实际上它不应该:
#include <string>
void foo(std::string const& oops = {})
{
}
int main()
{
foo();
}
Run Code Online (Sandbox Code Playgroud)
我知道两个可能有关的无声的错误代码错误:
老实说,我认为这些是不同的.有人知道吗
[1]使用C++ Console Application'向导'创建一个空项目.为简单起见,请禁用预编译标头并保留所有默认值:http://i.stack.imgur.com/rrrnV.png