【发布时间】:2012-08-27 01:26:08
【问题描述】:
可能重复:
What are Aggregates and PODs and how/why are they special?
C++11 中的 struct 需要什么样的构造函数才能将这个 struct 保持为 POD?
只接受初始化列表?或者可能没有任何限制?
【问题讨论】:
可能重复:
What are Aggregates and PODs and how/why are they special?
C++11 中的 struct 需要什么样的构造函数才能将这个 struct 保持为 POD?
只接受初始化列表?或者可能没有任何限制?
【问题讨论】:
你需要一个默认的默认构造函数,这样它就很简单了:
struct pot
{
constexpr pot() noexcept = default;
pot(int a, float b) : x(a), y(b) { }
int x;
float y;
};
constexpr 和 noexcept 是可选的,但我们也可以。
用法:
pot p; // OK
pot q(1, 1.5); // also OK
【讨论】: