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 const qualified type, otherwise false(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 |
Attention
If T is a reference type, is_const
Possible implementation
template<class T> struct is_const : std::false_type {}; template<class T> struct is_const<const T> : std::true_type {};
Check whether the type is const qualified
std::is_const
template< class T > |
(C++11 onwards) |
If T
is a const-qualified type (that is, const or const volatile ), provides a member constant value
that is equal to true. For any other type, value
is false .
Template parameters
T | – | Type to check |
Auxiliary variable template
template< class T > |
(C++17 onwards) |
Call example
#include <iostream> #include <type_traits> int main() { std::cout << std::boolalpha; std::cout << "std::is_const<int>::value: " << std::is_const<int>::value << std::endl; // false std::cout << "std::is_const<const int>::value: " << std::is_const<const int>::value << std::endl; // true std::cout << "std::is_const<const int*>::value: " << std::is_const<const int*>::value << std::endl; // false std::cout << "std::is_const<int* const>::value: " << std::is_const<int* const>::value << std::endl; // true std::cout << "std::is_const<const int & amp;>::value: " << std::is_const<const int & amp;>::value << std::endl; // false std::cout << "std::is_const<typename std::remove_reference<const int & amp;>::type>::value: " << std::is_const<typename std::remove_reference<const int & amp;>::type>::value << std::endl; // true return 0; }
Output
Check whether the type is volatile qualified
std::is_volatile
template< class T > |
(C++11 onwards) |
If T
is a volatile-qualified type (that is, volatile or const volatile ), then provides a member constant value
that is equal to true. For any other type, value
is false .
Template parameters
T | – | Type to check |
Auxiliary variable template
template< class T > |
(C++17 onwards) |
Possible implementation
template<class T> struct is_volatile : std::false_type {}; template<class T> struct is_volatile<volatile T> : std::true_type {};
Call example
#include <iostream> #include <type_traits> int main() { std::cout << std::boolalpha; std::cout << "std::is_volatile<int>::value: " << std::is_volatile<int>::value << std::endl; std::cout << "std::is_volatile<volatile int>::value:" << std::is_volatile<volatile int>::value << std::endl; return 0; }
Output
Check if the type is trivial
std::is_trivial
template< class T > |
(C++11 onwards) |
If T
is a trivial type (i.e. a scalar type, a trivial copyable class with a trivial default constructor, or an array of these classes/types, it can be cv qualified ), then provide a member constant value
that is equal to true. 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 > |
(C++17 onwards) |
Possible implementation
template< class T > struct is_trivial : std::integral_constant< bool, std::is_trivially_copyable<T>::value & amp; & amp; std::is_trivially_default_constructible<T>::value > {};
Call example
#include <iostream> #include <type_traits> struct A { int m; }; struct B { B() {} }; int main() { std::cout << std::boolalpha; std::cout << "std::is_trivial<A>::value: " << std::is_trivial<A>::value << std::endl; std::cout << "std::is_trivial<B>::value: " << std::is_trivial<B>::value << std::endl; return 0; }