C++ Standard Templates (STL) – Type Support (type attributes, is_floating_point, is_array, is_enum)

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

Basic type categories

Inherited from std::integral_constant

Member constants

value

[static]

If T is (can be cv qualified) std::nullptr_t 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 a floating point type

std::is_floating_point

template< class T >
struct is_floating_point;

(C++11 onwards)

Checks whether T is a floating point type. Provides member constant value equal to true if T is of type float , double , long double , including any cv-qualified variant. Otherwise, value is equal to false .

Template parameters
T Type to check
Auxiliary variable template

template< class T >
inline constexpr bool is_floating_point_v = is_floating_point::value;

(C++17 onwards)
Possible implementation
template< class T >
struct is_floating_point
     : std::integral_constant<
         bool,
         std::is_same<float, typename std::remove_cv<T>::type>::value ||
         std::is_same<double, typename std::remove_cv<T>::type>::value ||
         std::is_same<long double, typename std::remove_cv<T>::type>::value
     > {};

Check whether the type is an array type

std::is_array

template< class T >
struct is_array;

(C++11 onwards)

Checks whether T is of array type. If T is an array type, provide 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_array_v = is_array::value;

(C++17 onwards)
Possible implementation
template<class T>
struct is_array : std::false_type {};
 
template<class T>
struct is_array<T[]> : std::true_type {};
 
template<class T, std::size_t N>
struct is_array<T[N]> : std::true_type {};

Check whether the type is an enumeration type

std::is_enum

template< class T >
struct is_enum;

(C++11 onwards)

Checks whether T is an enumeration type. If T is an enumeration type, 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_enum_v = is_enum::value;

(C++17 onwards)

Call example

#include <iostream>
#include <type_traits>

class A {};

enum E {};

enum class Ec : int {};

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_floating_point<A>::value: "
              << std::is_floating_point<A>::value << std::endl;
    std::cout << "std::is_floating_point<float>::value: "
              << std::is_floating_point<float>::value << std::endl;
    std::cout << "std::is_floating_point<double>::value: "
              << std::is_floating_point<double>::value << std::endl;
    std::cout << "std::is_floating_point<long double>::value:"
              << std::is_floating_point<long double>::value << std::endl;
    std::cout << "std::is_floating_point<int>::value: "
              << std::is_floating_point<int>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_array<A>::value: "
              << std::is_array<A>::value << std::endl;
    std::cout << "std::is_array<A[]>::value: "
              << std::is_array<A[]>::value << std::endl;
    std::cout << "std::is_array<A[3]>::value: "
              << std::is_array<A[3]>::value << std::endl;
    std::cout << "std::is_array<float>::value: "
              << std::is_array<float>::value << std::endl;
    std::cout << "std::is_array<int>::value: "
              << std::is_array<int>::value << std::endl;
    std::cout << "std::is_array<int[]>::value: "
              << std::is_array<int[]>::value << std::endl;
    std::cout << "std::is_array<int[3]>::value: "
              << std::is_array<int[3]>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_enum<A>::value: "
              << std::is_enum<A>::value << std::endl;
    std::cout << "std::is_enum<E>::value: "
              << std::is_enum<E>::value << std::endl;
    std::cout << "std::is_enum<Ec>::value: "
              << std::is_enum<Ec>::value << std::endl;
    std::cout << "std::is_enum<int>::value: "
              << std::is_enum<int>::value << std::endl;
    std::cout << std::endl;

    return 0;
}
Output