考虑以下示例:
#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;
}
Run Code Online (Sandbox Code Playgroud)
这可以用g ++编译,但不能用clang ++编译,尝试使用以下命令: clang++ -std=c++11 -O0 main.cpp && ./a.out
从clang编译错误:
main.cpp:19:13:错误:const类型'const A'对象的默认初始化需要用户提供的默认构造函数
我从这个Stack Overflow问题中了解到,非POD类获得默认构造函数.这甚至不是必需的,因为变量具有c ++ 11样式的默认初始化
为什么这不是为了铿锵?为什么不起作用A() = default;?
| 归档时间: |
|
| 查看次数: |
2769 次 |
| 最近记录: |