C ++保证和名称为POD类数据,支持memcpy [英] C++ guarantee and name for POD-like data, memcpy capable

查看:194
本文介绍了C ++保证和名称为POD类数据,支持memcpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个问题中,我错误地使用术语POD来指代实际上不是POD类型(由于具有构造函数)的数据类型。现在,我已经通过标准,找不到一个正确的名称,我想要的。



我的意思是数据类型是一个POD,但可能包含函数,包括构造函数,但没有什么应该改变



在标准的第3.9节中,POD数据可以使用memcpy复制到另一个对象,或字符数据并返回。



但是,对象的对象表示在同一节中定义。它被定义为相信任何两个相同类型的对象可以通过memcpy安全地复制。



所以我的问题是:


  1. 是不是memcpy的副本确实对这些对象是安全的?

  2. 如果是的话, memcpy和POD?

  3. 这种类型的数据的名称是memcpy安全的吗?



< hr>

一个简单的对象类型示例我的意思是:

  struct ex_struct 
{
int a,b,c,d;
ex_struct():a(123){}
}






读取C ++ 0x草稿,我的结构将看起来像是一个简单的可复制类(9.1)。我相信这意味着memcpy将是安全的。

解决方案

在C ++ 0x中,PODness的概念被分为几个有用的类别:


一个简单的可复制类是一个类(draft 3242,section


  • 没有非平凡移动构造函数(12.8),

  • 没有非平凡的副本赋值运算符(13.5.3,12.8),

  • 没有非平凡的移动分配运算符(13.5.3,12.8),

  • 有一个简单的析构函数(12.4)。



  • 一个简单的类是一个具有琐碎的默认构造函数(12.1)的类,并且是可以复制的。



    [注意:特别是,一个简单的可复制或简单的类没有虚拟函数或虚拟基础
    类。 - 结束注释]



    标准布局类 是一个类:




    • 没有非标准布局类型(或此类数组)或引用的非静态数据成员,

    • 没有虚拟函数(10.3)和没有虚拟基本类(10.1),

    • 对所有非静态数据成员具有相同的访问控制(第11条) b $ b
    • 没有非标准布局基类,

    • 在大多数派生类中没有非静态数据成员,最多只有一个基类
      非静态数据成员,或没有具有非静态数据成员的基类,且

    • 没有与第一个非静态数据成员相同类型的基类。 li>

    对于简单构造函数,赋值运算符和析构函数的要求分散在第12节特殊成员函数 [special]


    In another question I incorrectly used the term POD to refer to data types that aren't actually POD types (on account of having a constructor). Now, I've looked through the standard and couldn't find a proper name for what I want. Nor can I actually find a guarantee that copying is actually allowed.

    The data type I mean is a POD, but may contain functions, including constructors, but nothing that should alter its alignment or size characteristics when compared to an equivalent POD type.

    In section 3.9 of the standard it states that POD data can be copied with memcpy, either to another object, or to character data and back. No such guarantee is ever made of non-POD data.

    However, the object representation of an object is defined in the same section. It is defined such that one would believe any two objects of the same type could be safely copied via memcpy.

    So my questions are:

    1. Is the copy with memcpy actually guaranteed to be safe for such objects?
    2. If yes, then why is there a special note about memcpy and POD?
    3. Is there a name for this type of data which is memcpy safe?


    A simple example of the type of object I mean:

    struct ex_struct
    {
      int a,b,c,d;
      ex_struct() : a(123) { }
    }
    


    Reading the C++0x draft, my struct would appear to be a trivially copyable class (9.1). I believe that implies memcpy would be safe.

    解决方案

    In C++0x, the concept of PODness is broken out into several individually useful categories:

    A trivially copyable class is a class that (draft 3242, section [class]):

    • has no non-trivial copy constructors (12.8),
    • has no non-trivial move constructors (12.8),
    • has no non-trivial copy assignment operators (13.5.3, 12.8),
    • has no non-trivial move assignment operators (13.5.3, 12.8), and
    • has a trivial destructor (12.4).

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

    [ Note: In particular, a trivially copyable or trivial class does not have virtual functions or virtual base classes. — end note ]

    A standard-layout class is a class that:

    • has no non-static data members of type non-standard-layout class (or array of such types) or reference,
    • has no virtual functions (10.3) and no virtual base classes (10.1),
    • has the same access control (Clause 11) for all non-static data members,
    • has no non-standard-layout base 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, and
    • has no base classes of the same type as the first non-static data member.

    The requirements for trivial constructors, assignment operators, and destructor are scattered throughout section 12 "Special Member Functions" [special].

    这篇关于C ++保证和名称为POD类数据,支持memcpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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