This seems to work on my compiler but is it standard? Should I do a memcpy instead?
struct Foo { u32 a, b; u8 c[24]; }
class Bar {
Foo f;
public:
Bar(Foo&f):f(f){}
};
This seems to work on my compiler but is it standard? Should I do a memcpy instead?
struct Foo { u32 a, b; u8 c[24]; }
class Bar {
Foo f;
public:
Bar(Foo&f):f(f){}
};
In C++11 International Standard, struct Foo
is regarded as POD
(Plain Old Data, refer to clause 9 spec#10), and it is trivially copyable type
(refer to clause 3.9 spec#9).
the bitwise-copying is normally determined by the size of the the type, for small types, it may be optimized as a simple movl/movq
instruction. The compiler will do the job, thus you do not need memcpy
in the constructor block of class Bar
, since you have set the value in the initializer list Bar(Foo&f):f(f)
.