【发布时间】:2022-01-20 05:51:46
【问题描述】:
std::is_trivially_copyable 有一个演示代码 https://en.cppreference.com/w/cpp/types/is_trivially_copyable
void test()
{
struct A {
int m;
A(const A& o):m(o.m){}
};
struct D {
int m;
D(D const&) = default; // -> trivially copyable
D(int x) : m(x + 1) {}
};
std::cout << std::is_trivially_copyable<A>::value << '\n';
std::cout << std::is_trivially_copyable<D>::value << '\n';
}
A 不可复制,D 可以。我用默认行为实现了 A 的复制构造函数。造成这种差异的原因是什么?
【问题讨论】: