这是C ++ 11中的结构POD吗?

时间:2011-08-24 02:01:45

标签: c++ pod c++11

这个结构是C ++ 11中的POD吗?

struct B
{
  int a;
  B(int aa) : a(aa) {}
  B() = default;
};

请注意,这个问题是关于 C ++ 11 的明确问题。我知道这个类不是C ++ 98中的POD,也不是C ++ 03。

有关C ++ 11中POD的说明,请参阅trivial vs. standard layout vs. POD

(灵感来自这个问题:Is there a compile-time func/macro to determine if a C++0x struct is POD?

1 个答案:

答案 0 :(得分:14)

是的,根据new rules,它是POD。

如果你查看新标准的第§8.4.2/ 4段,你可以看到如果构造函数在第一个声明中是默认的,那么它不是用户提供的:

  

明确默认的函数和隐式声明的函数   统称为默认函数,实现应该   为它们提供隐式定义(§12.1§12.4,§12.8),这可能是   意味着将它们定义为已删除一个特殊的成员函数是   用户提供的,如果它是用户声明的,并且没有明确默认或   在第一份声明中删除。 (...)

您可以使用std::is_pod类型特征为static_assert提供编译器test this

static_assert(std::is_pod<B>::value, "B should be a POD");