7

The following code

#include <iostream>
#include <new>
#include <cstring>
#include <type_traits>

template<typename T>
void is_pod(char* c)
{
    cout << "Type " << c;
    if(std::is_pod<T>::value)
        ::std::cout << " is POD" << endl;
    else
        ::std::cout << " is not!" << endl;
}

#define CHECK_TYPE(ty) ::is_pod<ty>(#ty)

struct POD_Parent{};
struct POD_Child : public POD_Parent{int y;};
struct POD_Child2 {int x; POD_Parent y; POD_Child ssd;};

int main()
{
    CHECK_TYPE(POD_Parent);
    CHECK_TYPE(POD_Child);
    CHECK_TYPE(POD_Child2);

Gives the following results: Which is strange!

Type POD_Parent is POD
Type POD_Child is not!
Type POD_Child2 is POD

How can POD_Child is not POD?! and POD_Child2 is POD?!!

Note that I compiled it using MinGW (using option -std=c++11) and it said that all of them are POD.

4

1 に答える 1

0

According to [MSDN][1] a type that has a base class is not POD so POD_Child is not POD but for POD_Child2 its possibly some mistake of the compiler that ignore base class of ssd

于 2013-02-19T16:58:19.680 に答える