I would like to know what type introspection I can do to detect types that assignable by simply raw memory copy?
For example, as far I understand, built-in types tuples
The correct test is in fact std::is_trivially_copyable
, which allows use of memcpy
for both making a new object and modifying an existing one.
Although you may be surprised that these return false for types where your intuition tells you that memcpy
ought to be ok, they are not lying; the Standard indeed makes memcpy
undefined behavior in these cases.
In the particular case of std::pair
, we can get some insight into what goes wrong:
int main()
{
typedef std::pair P;
std::cout << "\nTC: " << std::is_trivially_copyable::value;
std::cout << "\nTCC: " << std::is_trivially_copy_constructible
::value;
std::cout << "\nTCv: " << std::is_trivially_constructible
::value;
std::cout << "\n CC: " << std::is_copy_constructible
::value;
std::cout << "\n MC: " << std::is_move_constructible
::value;
std::cout << "\nTCA: " << std::is_trivially_copy_assignable
::value;
std::cout << "\nTCvA:" << std::is_trivially_assignable
::value;
std::cout << "\n CA: " << std::is_copy_assignable
::value;
std::cout << "\n MA: " << std::is_move_assignable
::value;
std::cout << "\nTD: " << std::is_trivially_destructible
::value;
}
TC: 0 TCC: 1 TCv: 1 CC: 1 MC: 1 TCA: 0 TCvA:0 CA: 1 MA: 1 TD: 1
Evidently it isn't trivially copy assignable.1
The pair
assignment operator is user-defined, so not trivial.
1I think that clang, gcc, and msvc are all wrong here, actually, but if it satisfied std::_is_trivially_copy_assignable
it wouldn't help, because TriviallyCopyable requires that the copy constructor, if not deleted, is trivial, and not the TriviallyCopyAssignable trait. Yeah, they're different.
A copy/move assignment operator for class X is trivial if it is not user-provided and...
vs
is_assignable_v
is true and the assignment, as defined byis_assignable
, is known to call no operation that is not trivial.
The operations called by pair
's copy assignment operator are the assignments of individual doubles, which are trivial.
Unfortunately, the definition of trivially copyable relies on the first, which pair
fails.
A trivially copyable class is a class:
- where each copy constructor, move constructor, copy assignment operator, and move assignment operator is either deleted or trivial,
- that has at least one non-deleted copy constructor, move constructor, copy assignment operator, or move assignment operator, and
- that has a trivial, non-deleted destructor.