std::is_pod is deprecated since C++20 and the answer to this question tells us that we should use std::is_standard_layout + std::is_trivial instead.
However, with C++26, now also std::is_trivial is deprecated. Why is that and what should we use instead?
This change comes from P3247: Deprecate the notion of trivial types.
The problem with std::is_trivial is that it mixes two different checks:
std::is_trivially_copyablestd::is_trivially_default_constructiblestd::is_trivial is somewhat uselessThese two checks are used in different situations, so having them bunched together into a single trait isn't very useful.
For example, a container that explicitly manages lifetimes of objects stored in a std::byte[] (likestd::vector)
std::memcpy for trivially copyable types, andstd::memcpy for trivially copy-assignable types.Knowing whether a type is trivial is somewhat useless because we're typically interested in more specific properties.
Of course, there are some rare situations where we want to know both properties and std::is_trivial provides a bit of convenience, but it's not a lot of work to just ask for trivial copyability and trivial default construction separately.
std::is_trivial may be a pitfallA developer may assume that if std::is_trivial is true, a type is "totally trivial" in every regard, including assignment.
However, the proposal provides a counter-example:
struct S { const int i; };
Sis trivially copyable, yet it is not assignable. A user seeking to replace copy-assignment withmemcpyshould useis_trivially_copy_assignablefor the check; checking justis_trivialoris_trivially_copyableas a precondition for assignment would underconstrain the user code.
Note that S is trivial, in case this wasn't clear.
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