C++ Standard Templates (STL) – Type Support (type attributes, is_volatile, is_trivial, is_const)

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 header results in undefined behavior, except that std::common_type can be specialized as described.

Templates defined in the header file may be instantiated with incomplete types unless otherwise specified, although instantiating standard library templates with incomplete types is generally prohibited.

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::value is always false. The correct way to check the constancy of a type that may be a reference is to remove the reference: is_const::type> .

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 >
struct is_const;

(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 >
inline constexpr bool is_const_v = is_const::value;

(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 >
struct is_volatile;

(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 >
inline constexpr bool is_volatile_v = is_volatile::value;

(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 >
struct is_trivial;

(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 is an incomplete type other than void (which may be cv qualified).

Template parameters
T Type to check
Auxiliary variable template

template< class T >
inline constexpr bool is_trivial_v = is_trivial::value;

(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;
}
Output