I wonder what the purpose of the type traits std::is_trivially_copy_assignable
, std::is_trivially_copy_constructible
, std::is_trivially_move_assignable
and std::is_trivially_move_constructible
if you can't exploit them for initialization or assignment with memcpy
They tell you properties of the type, isn't that enough reason to exist?
You might have a type which has a trivial copy constructor but a non-trivial move constructor, so it would not qualify as a trivially copyable type, but is trivially-copy-constructible.
When using such a type you could use SFINAE or other static polymorphism to enable/disable certain operations unless they're guaranteed to be trivial. For example imagine a class template TrivialPair<A,B>
which could declare its move constructor as deleted if A
and B
are not trivially move constructible, and similarly for the other operations. That would mean that TrivialPair
only supports operations which A
and B
both support and which don't call any non-trivial functions.
I also wonder why the standard requires a trivial destructor for values to be copyable with memcpy
"Trivially copyable" can be thought of as saying the type is just a bunch of bytes, i.e. just data that can be copied to another place in memory safely and without altering the value. If the type has a non-trivial destructor then it's not just a bunch of bytes, it has some additional behaviour which the compiler doesn't comprehend, and which might make using memcpy
unsafe.