Do the padding bytes of a POD type get copied?

后端 未结 4 1114
忘了有多久
忘了有多久 2021-01-03 17:21

Suppose I have a POD type like this:

struct A {
    char a;
    int b;
};

On my system, sizeof(A) == 8, even though size

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 18:14

    given that you asked about a POD type ( hence including unions ) it's worth mentioning that according to [class.copy]

    The implicitly-defined copy/move constructor for a union X copies the object representation (6.9) of X

    that for trivially copyable types should include padding bits as well. So, it could be just a matter of replacing A with

    union A{ struct {
        char a;
        int b;
    }; };
    

    (actually, the above uses a non standard anonymous struct, but you get the point ... )

提交回复
热议问题