C++ Standard Templates (STL) – Type Support (type attributes, is_void, is_null_pointer, is_integral)

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 causes 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 instantiation of standard library templates with incomplete types is generally prohibited.

Type attribute

Defined in header file

Basic type categories

Inherited from std::integral_constant

Member constants

value

[static]

If T is an integer type, it is true, otherwise it is 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

Check whether the type is void

std::is_void

template< class T > struct is_void;

Checks whether T is of type void. Provides member constant value equal to true if T is of type void , const void , volatile void , or const volatile void . Otherwise, value is equal to false .

Template parameters
T Type to check
Auxiliary variable template

template< class T >
inline constexpr bool is_void_v = is_void::value;

(C++17 onwards)
Possible implementation
template< class T >
struct is_void : std::is_same<void, typename std::remove_cv<T>::type> {};

Check whether the type is std::nullptr_t

std::is_null_pointer

template< class T >
struct is_null_pointer;

(C++11 onwards)

Checks whether T is of type std::nullptr_t.

If T is of type std::nullptr_t, const std::nullptr_t, volatile std::nullptr_t, or const volatile std::nullptr_t, provide a member constant value equal to true .

Otherwise, value is equal to false .

Template parameters
T Type to check
Auxiliary variable template

template< class T >
inline constexpr bool is_null_pointer_v = is_null_pointer::value;

(C++17 onwards)
Possible implementation
template< class T >
struct is_null_pointer: std::is_same<std::nullptr_t,
typename std::remove_cv<T>::type> {};
Attention

For std::nullptr_t , std::is_pointer is false because it is not a built-in pointer type.

Check whether the type is an integer

std::is_integral

template< class T >
struct is_integral;

(C++11 onwards)

Checks whether T is of type integer. If T is bool, char, char8_t, char16_t, char32_t, wchar_t, short, int, long, long long, or any implementation-defined extended integer type, including any signed, unsigned, and cv Limited variants. then provides a member constant value that is equal to true. Otherwise, value is equal to false .

Template parameters
T Type to check
Auxiliary variable template

template< class T >
inline constexpr bool is_integral_v = is_integral::value;

(C++17 onwards)

Call example

#include <iostream>
#include <type_traits>

class A {};

enum E : int {};

template <class T>
T f(T i)
{
    static_assert(std::is_integral<T>::value, "Integral required.");
    return i;
}

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_void<void>::value: "
              << std::is_void<void>::value << std::endl;
    std::cout << "std::is_void<int>::value: "
              << std::is_void<int>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_null_pointer<decltype(nullptr)>::value: "
              << std::is_null_pointer<decltype(nullptr)>::value << std::endl;
    std::cout << "std::is_null_pointer< int* >::value: "
              << std::is_null_pointer< int* >::value << std::endl;
    std::cout << "std::is_pointer< decltype(nullptr) >::value: "
              << std::is_pointer< decltype(nullptr) >::value << std::endl;
    std::cout << "std::is_pointer<int*>::value: "
              << std::is_pointer<int*>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_integral<A>::value: "
              << std::is_integral<A>::value << std::endl;
    std::cout << "std::is_integral<E>::value: "
              << std::is_integral<E>::value << std::endl;
    std::cout << "std::is_integral<float>::value: "
              << std::is_integral<float>::value << std::endl;
    std::cout << "std::is_integral<int>::value: "
              << std::is_integral<int>::value << std::endl;
    std::cout << "std::is_integral<bool>::value: "
              << std::is_integral<bool>::value << std::endl;
    std::cout << f(123) << std::endl;

    return 0;
}
Output