Is memcpy struct safe with arrays and some methods?

I am wondering if memcpy can be used in a structure containing arrays and methods (only some getters and setters, since indexing arrays is unusual and I have to match it somehow). I know that it is safe for POD, and I'm not sure if my structure will be considered POD or not?

+5
source share
1 answer

You can use memcpy if struct TriviallyCopyable .

You can check if your struct trivially std::is_trivially_copyable using std::is_trivially_copyable .

In addition, as @JohanLundberg noted in the comment, the destination address must be 0 modulo std::alignment_of<T> . You can learn more about object alignment requirements at http://en.cppreference.com/w/cpp/language/object#Alignment .

+10
source

Source: https://habr.com/ru/post/1236085/


All Articles