Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 is_pod with GCC 4.6

Tags:

c++

gcc

c++11

Under the relaxed definition of POD in C++11, it is my understanding that the following struct is considered a POD:

template <class T>
struct Foo 
{
    Foo()
    { }

    explicit Foo(T* obj) : m_data(obj)
    { }

    T* m_data;
};

However, using GCC 4.6 and compiling with the -std=c++0x flag, if I say:

std::cout << std::boolalpha << std::is_pod<Foo<int>>::value << std::endl;

It outputs:

false

Here is an ideone link showing the full program. (Note that ideone uses GCC 4.5)

So, is my understanding of PODs in C++11 mistaken, or is GCC 4.6 simply not up-to-date in terms of C++11 compliance?

like image 340
Channel72 Avatar asked Jan 27 '13 02:01

Channel72


2 Answers

A POD struct must be a trivial class (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 defines what a trivial class is:

A trivial class is a class that has a trivial default constructor and is trivially copyable.

§12.1[class.ctor]/5 defines what a trivial default constructor is. It begins:

A default constructor is trivial if it is not user-provided and...

The default constructor of Foo<T> is user-provided and is therefore nontrivial. Therefore, Foo<int> is not POD. It is, however, standard layout.

like image 80
James McNellis Avatar answered Oct 07 '22 17:10

James McNellis


Default declaring default constructor, makes Foo a POD. i.e.

Foo() = default;
explicit Foo(T* obj) : m_data(obj)
{ }

http://ideone.com/vJltmA

like image 35
balki Avatar answered Oct 07 '22 16:10

balki