这是struct POD在C ++ 11? [英] Is this struct POD in C++11?
问题描述
这个结构是C ++ 11中的POD吗?
Is this struct a POD in C++11?
struct B
{
int a;
B(int aa) : a(aa) {}
B() = default;
};
请注意,这个问题是关于 C ++ 11 的。我知道这个类不是C ++ 98和C ++ 03中的POD。
Note that this question is explicit about C++11. I know that this class is not a POD in C++98 nor C++03.
有关C ++ 11中POD的说明,请参阅平凡与标准版面对比POD
For an explanation of POD in C++11, see trivial vs. standard layout vs. POD
(受此问题启发:是否有编译时func /宏来确定C ++ 0x结构是否是POD?)
推荐答案
Yes, it is a POD according to the new rules.
如果你查阅段落8.4.2节/ 4节的新标准,你可以看到如果一个构造函数默认在第一个声明,它不是用户提供:
If you look up paragraph §8.4.2/4 of the new standard, you can see that if a constructor is defaulted on the first declaration, it is not user-provided:
默认函数和隐式声明的函数是
,统称为 defaulted 函数,实现
为它们提供隐式定义(&12; 12; 12.4,&这可能
意味着将它们定义为已删除。如果是用户声明的并且没有明确默认或者在第一个声明中删除
,则特殊成员函数是
用户提供的。 (...)
Explicitly-defaulted functions and implicitly-declared functions are collectively called defaulted functions, and the implementation shall provide implicit definitions for them (§12.1 §12.4, §12.8), which might mean defining them as deleted. A special member function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. (...)
您可以使用 std :: is_pod
类型trait让编译器用 static_assert
测试此。
You can use the std::is_pod
type trait to have the compiler test this for you with static_assert
.
static_assert(std::is_pod<B>::value, "B should be a POD");
这篇关于这是struct POD在C ++ 11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!