pod

作者: 秋冬不寒 | 来源:发表于2022-05-22 23:33 被阅读0次

    ## POD 类型

    当某一类或结构同时为普通和标准布局时,该类或结构为 POD(简单旧数据)类型。 因此,POD 类型的内存布局是**连续**的,并且每个成员的地址都比在其之前声明的成员要高,以便可以对这些类型执行逐字节复制和二进制 I/O。 标量类型(例如 int)也是 POD 类型。 作为类的 POD 类型只能具有作为非静态数据成员的 POD 类型。

    ## [](https://docs.microsoft.com/zh-cn/cpp/cpp/trivial-standard-layout-and-pod-types?view=msvc-150#example)示例

    以下示例演示普通、标准布局和 POD 类型之间的区别:

    ```

    #include <type_traits>

    #include <iostream>

    using namespace std;

    struct B

    {

    protected:

      virtual void Foo() {}

    };

    // Neither trivial nor standard-layout

    struct A : B

    {

      int a;

      int b;

      void Foo() override {} // Virtual function

    };

    // Trivial but not standard-layout

    struct C

    {

      int a;

    private:

      int b;  // Different access control

    };

    // Standard-layout but not trivial

    struct D

    {

      int a;

      int b;

      D() {} //User-defined constructor

    };

    struct POD

    {

      int a;

      int b;

    };

    int main()

    {

      cout << boolalpha;

      cout << "A is trivial is " << is_trivial<A>() << endl; // false

      cout << "A is standard-layout is " << is_standard_layout<A>() << endl;  // false

      cout << "C is trivial is " << is_trivial<C>() << endl; // true

      cout << "C is standard-layout is " << is_standard_layout<C>() << endl;  // false

      cout << "D is trivial is " << is_trivial<D>() << endl;  // false

      cout << "D is standard-layout is " << is_standard_layout<D>() << endl; // true

      cout << "POD is trivial is " << is_trivial<POD>() << endl; // true

      cout << "POD is standard-layout is " << is_standard_layout<POD>() << endl; // true

      return 0;

    }

    ```

    ## [](https://docs.microsoft.com/zh-cn/cpp/cpp/trivial-standard-layout-and-pod-types?view=msvc-150#literal_types)文本类型

    文本类型是可在编译时确定其布局的类型。 以下均为文本类型:

    *  void

    *  标量类型

    *  引用

    *  Void、标量类型或引用的数组

    *  具有普通析构函数以及一个或多个 constexpr 构造函数且不移动或复制构造函数的类。 此外,其所有非静态数据成员和基类必须是文本类型且不可变。

    相关文章

      网友评论

          本文标题:pod

          本文链接:https://www.haomeiwen.com/subject/opilprtx.html