【问题标题】:What rules do I have to follow in order to ensure that a C++ type is POD? [duplicate]为了确保 C++ 类型是 POD,我必须遵循哪些规则? [复制]
【发布时间】:2012-10-25 00:48:57
【问题描述】:

可能重复:
What are POD types in C++?

What are Aggregates and PODs and how/why are they special?

我正在用 C++ 编写解释器,我想确保某些 C++ 数据类型在通过解释代码访问时具有可预测的布局,尤其是在使用反射时。例如,我想确保第一个数据字段始终位于对象地址的零偏移处。现在,这对于纯 POD 类型来说是微不足道的。但是,我想知道这是否也适用于具有继承或构造函数的对象,只要我避免像虚函数或多重继承这样的明显事物。假设编译器将以与 C 编译器相同的方式布局这些类型是否合理,或者 C++ 标准中的“未指定行为”是否是我需要担心的事情?

【问题讨论】:

标签: c++ inheritance compiler-construction


【解决方案1】:

c++11 定义了standard layout

标准布局旨在捕捉第一个意图——使用与 C 中相同的布局创建一些东西

这就是你要找的。因此,您的支票应该是:

static_assert( std::is_standard_layout<A>::value, "not standard layout" );

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 2010-09-13
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多