【发布时间】:2015-02-05 06:59:04
【问题描述】:
考虑以下示例:
#include <iostream>
#include <type_traits>
struct A
{
//A() = default; // does neither compile with, nor without this line
//A(){}; // does compile with this line
int someVal{ 123 };
void foobar( int )
{
};
};
int main()
{
const A a;
std::cout << "isPOD = " << std::is_pod<A>::value << std::endl;
std::cout << "a.someVal = " <<a.someVal << std::endl;
}
这个用g++编译但是用clang++编译不出来,用下面的命令试试:clang++ -std=c++11 -O0 main.cpp && ./a.out
clang 编译错误:
main.cpp:19:13: 错误:const 类型“const A”的对象的默认初始化需要用户提供的默认构造函数
我从This Stack Overflow Question 了解到,非 POD 类获得默认构造函数。这在这里甚至没有必要,因为该变量具有 c++11 风格的默认初始化
为什么这不适合clang?
为什么A() = default; 也不起作用?
【问题讨论】: