为什么这不是一个POD类型? [英] Why isn't this a POD type?

查看:152
本文介绍了为什么这不是一个POD类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在g ++ 4.6.2(mingw)上使用 g ++ -std = c ++ 0x pod_test.cpp 运行下面的代码。我在A4上遇到错误。为什么不是A4 POD?

I ran the below with g++ -std=c++0x pod_test.cpp on g++ 4.6.2 (mingw). I get an error on A4. Why isn't A4 POD?

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

using namespace std;

struct A {
    int a, b;
    char c;
};
struct A2 {
    short buf[1];
};
struct A3:A {
};
struct A4:A {
    short buf[1];
};
static_assert(std::is_pod<A>::value, "Struct must be a POD type");
static_assert(std::is_pod<A2>::value, "Struct must be a POD type");
static_assert(std::is_pod<A3>::value, "Struct must be a POD type");
static_assert(std::is_pod<A4>::value, "Struct must be a POD type");

int main(){}


推荐答案

这不是POD,因为它打破了这个标准布局类的规则:

It's not POD because it breaks this rule for standard layout classes:


- 最多没有非静态数据成员派生类和
至多一个具有非静态数据成员的基类,或没有带非静态数据成员的

— either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members

继承网格中只有一个类可以具有非静态数据成员。在这种情况下,
A A4 都有。

Only one class in the inheritance lattice can have non-static data members. In this case, both A and A4 have.

这篇关于为什么这不是一个POD类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆