在relaxed definition of POD下在 C++11 中,我的理解是以下结构被视为 POD:
template <class T>
struct Foo
{
Foo()
{ }
explicit Foo(T* obj) : m_data(obj)
{ }
T* m_data;
};
但是,使用 GCC 4.6 并使用 -std=c++0x
标志进行编译,如果我说:
std::cout << std::boolalpha << std::is_pod<Foo<int>>::value << std::endl;
输出:
false
这是一个 ideone link显示完整的程序。 (注意ideone使用的是GCC 4.5)
那么,我对 C++11 中 POD 的理解是错误的,还是 GCC 4.6 在 C++11 合规性方面根本不是最新的?
最佳答案
POD 结构必须是普通类 (C++11 §9[class]/10):
A POD struct is a non-union class that is both a trivial class and a standard-layout class, and has no non-static data members of type non-POD struct, non-POD union (or array of such types).
§9[class]/6 定义了普通类:
A trivial class is a class that has a trivial default constructor and is trivially copyable.
§12.1[class.ctor]/5 定义了普通默认构造函数是什么。它开始:
A default constructor is trivial if it is not user-provided and...
Foo<T>
的默认构造函数是用户提供的,因此是重要的。因此,Foo<int>
不是 POD。但是,它是标准布局。
关于C++11 is_pod 与 GCC 4.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14543983/