C++11 is_pod 与 GCC 4.6

标签 c++ c++11 gcc

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/

相关文章:

c++ - 如果要调用多个成员函数,对象编辑器是一个好方法吗?

c++ - OpenMP 刷新与刷新(列表)

c++ - 作为非类型名参数的局部变量

c++ - 使用 Boost + GCC + 预编译 header 的编译时间变慢

macos - 在 mac 上安装 GNU GCC

c++ - Qt - 确定绝对小部件和光标位置

c++ - 为模板化函数定义特定情况 (C++)

c++ - 专门化模板类构造函数

c++11 - 移动构造函数和 const 成员变量

c++ - GCC 内部编译器错误 : Segmentation fault