Type attributes
A type attribute defines a compile-time template-based structure for querying or modifying the properties of a type.
Attempting to specialize a template defined in the
Templates defined in the
Type attribute
Defined in header file
Inherited from std::integral_constant
Member constants
value [static] |
True if T is a trivially copyable object, false otherwise(Public static member constants) |
Member functions
operator bool |
Convert the object to bool and return value (Public member function) |
operator() (C++14) |
Return value (Public member function) |
Member type
Type | Definition |
value_type |
bool |
type |
std::integral_constant |
Checks whether the type can be trivially copied
std::is_trivially_copyable
template< class T > |
(C++11 onwards) |
If T
is of type TriviallyCopyable, provide member constant value
equal to true. For any other type, value
is false .
The only trivially copyable types are scalar types, trivially copyable classes, and arrays of these types/classes (can be cv qualified).
The behavior is undefined if std::remove_all_extents_t
Template parameters
T | – | Type to check |
Auxiliary variable template
template< class T > |
(C++17 onwards) |
Attention
Trivially copyable objects are the only objects that can be safely copied with std::memcpy or serialized from/to a binary file with std::ofstream::write()/std::ifstream::read().
In general, for any trivially copyable object obj1
of type T
and T
, the underlying word of obj1
can be copied section (for example, with std::memcpy or std::memmove) to an array of char
, unsigned char
, or std::byte, or to T
in a different object obj2
. Neither obj1
nor obj2
can be potentially overlapping sub-objects.
If you copy the underlying bytes of obj1
into such an array, and then copy the resulting contents back into obj1
, then obj1
will retain its original value. If the underlying bytes of obj1
are copied to obj2
, then obj2
will retain the value of obj1
.
Call example
#include <iostream> #include <type_traits> struct A { int m; }; struct B { B(const B & amp;) {} }; struct C { virtual void foo(); }; int main() { std::cout << std::boolalpha; std::cout << "std::is_trivially_copyable<A>::value: " << std::is_trivially_copyable<A>::value << std::endl; std::cout << "std::is_trivially_copyable<B>::value: " << std::is_trivially_copyable<B>::value << std::endl; std::cout << "std::is_trivially_copyable<C>::value: " << std::is_trivially_copyable<C>::value << std::endl; return 0; }
Output
Checks whether it is a standard layout type
std::is_standard_layout
template< class T > |
(C++11 onwards) |
If T
is a standard layout type (that is, a scalar type, a standard layout class, or an array of this type/class, which can be cv qualified), provide a member constant value that is equal to true code>. For any other type,
value
is false .
A standard layout class is a class that meets the Standard Layout Type (StandardLayoutType).
The behavior is undefined if std::remove_all_extents_t
Template parameters
T | - | Type to check |
Auxiliary variable template
template< class T > |
(C++17 onwards) |
Attention
A pointer to a standard layout class can be converted (by reinterpret_cast ) to a pointer to its first non-static data member, and vice versa.
If a standard layout union holds two or more standard layout structures, viewing their common leading parts is allowed.
The macro offsetof can only be used with standard layout classes.
Call example
#include <iostream> #include <type_traits> struct A { int m; }; struct B { int m1; private: int m2; }; struct C { virtual void foo(); }; int main() { std::cout << std::boolalpha; std::cout << "std::is_standard_layout<A>::value: " << std::is_standard_layout<A>::value << std::endl; std::cout << "std::is_standard_layout<B>::value: " << std::is_standard_layout<B>::value << std::endl; std::cout << "std::is_standard_layout<C>::value: " << std::is_standard_layout<C>::value << std::endl; return 0; }
Output
Check whether the type is a plain old data (POD) type
std::is_pod
template< class T > |
(since C++11) (Deprecated in C++20) |
If T
is a plain old data type (PODType) ("plain old data type"), that is, both trivial and standard layout, then provide a member constant equal to truevalue
. For any other type, value
is false .
The behavior is undefined if std::remove_all_extents_t
Template parameters
T | - | Type to check |
Auxiliary variable template
template< class T > |
(since C++17) (Deprecated in C++20 |
Attention
POD type objects are fully compatible with the C programming language.
Call example
#include <iostream> #include <type_traits> struct A { int m; }; struct B { int m1; private: int m2; }; struct C { virtual void foo(); }; int main() { std::cout << std::boolalpha; std::cout << "std::is_pod<A>::value: " << std::is_pod<A>::value << std::endl; std::cout << "std::is_pod<B>::value: " << std::is_pod<B>::value << std::endl; std::cout << "std::is_pod<C>::value: " << std::is_pod<C>::value << std::endl; return 0; }