When is a type in c++11 allowed to be memcpyed?

后端 未结 5 512
滥情空心
滥情空心 2020-11-30 05:48

My question is the following:

If I want to copy a class type, memcpy can do it very fast. This is allowed in some situations.

We have some type traits:

5条回答
  •  遥遥无期
    2020-11-30 06:17

    You can copy an object of type T using memcpy when is_trivially_copyable::value is true. There is no particular need for the type to be a standard layout type. The definition of 'trivially copyable' is essentially that it's safe to do this.

    An example of a class that is safe to copy with memcpy but which is not standard layout:

    struct T {
      int i;
    private:
      int j;
    };
    

    Because this class uses different access control for different non-static data members it is not standard layout, but it is still trivially copyable.

提交回复
热议问题