Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moving an object in memory using std::memcpy

Tags:

c++

c++11

Is it allowed to move a class instance object from one location to another (for example, by using std::memcpy or std::memove? Assume both source and destination locations have the same alignment. Then casting the destination "object" into the type of the source object and calling into it. What part of the C++11 standard forbids this?

like image 551
user1095108 Avatar asked Feb 14 '26 08:02

user1095108


1 Answers

You should consider the template is_trivially_copyable provided by C++11 that is useful to understand if it safe to memcpy a given type. If the value is false then the result is undefined.

like image 111
Jack Avatar answered Feb 16 '26 23:02

Jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!