std::cout << std::boolalpha;
std::cout << std::is_trivially_copyable< std::pair<const int,int> >::value;
std::cout << std::is_trivially_copyable< std::pair<int,int> >::value;
When I use GCC 9.2 the output is true
false
.
When I use Clang 5.0 or GCC 5.2 the output is false
false
.
Why the difference?
std::pair
has a non-trivial copy-assignment and move-assignment operator. This prevents it from being trivially copyable.
Since C++17, if one of the two contained types is not assignable, then the copy/move assignment operator is defined as deleted, which lifts this restriction on being trivially copyable. This is the case here because const int
is not copy-assignable or move-assignable.
C++17 also states that if the two types have trivial destructors, then the pair will also have a trivial destructor, which is another requirement for being trivially copyable.
The older compilers you tested probably do not have full support for C++17, which prevents the pair from being trivially copyable even for pair<const int, int>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With