...

/

Exploring the Standard Type Traits: Part 2

Exploring the Standard Type Traits: Part 2

Understand type transformation using metafunctions in C++ template metaprogramming.

Modifying cv-specifiers, references, pointers, or a sign

The type traits that are performing transformations on types are also called metafunctions. These type traits provided a member type (typedef) called type that represents the transformed type. This category of type traits includes the following:

Type Traits for Certain Transformations

Name

Description

add_cv

add_const

add_volatile

Add the const, volatile, or both specifiers to a type.

remove_cv

remove_const

remove_volatile

Remove the const, volatile, or both specifiers from a type.

add_value_reference

add_rvalue_reference

Add an Ivalue or value reference to a type.

remove_reference

Removes a reference (either value or rvalue) from a type.

remove_cvref

Removes the const and volatile specifiers as well as value or value references from a type. It combines the remove_cv and remove_reference traits.

add_pointer

Adds a pointer to a type.

remove_pointer

Removes a pointer from a type.

make_signed

make_unsigned

Make an integral type (except for bool) or an enumeration type either signed or unsigned. The supported integral types are short, int, long, long long, char, whar_t, char8_t, char16_t, and char32_t.

remove_extent

remove_all_extents

Remove one or all extents from an array type.

With the exception of remove_cvref, which was added in C++20, all the other type traits listed in this table are available in C++11. These aren’t all the metafunctions from the standard library. More are listed next.

Miscellaneous transformations

Apart from the metafunctions previously listed, there are other type traits performing type transformations. The most important of these are listed in the following table:

Type Traits for Miscellaneous Transformationas

Name

C++ Version

Description

enable_if

C++11

Enables the removal of a function overload or template specialization from overload resolution.

conditional

C++11

Defines the member type called type to be one of two possible types by selecting them based on a compile-time boolean condition.

decay

C++11

Applies transformations on a type (array-to-pointer for array types, Ivalue-to-rvalue for reference types, and function-to-pointer for function types), removes const and volatile qualifiers, and uses the resulting member typedef type as its own member typedef type.

common_type

C++11

Determines the common type from a group of types.

common_reference

C++20

Determines the common reference type from a group of types.

underlying_type

C++11

Determines the underlying type of an enumeration type.

void_t

C++17

A type alias mapping a sequence a of types to the void type.

type_indentity

C++20

Provides the member typedef type as an alias for the type argument T.

...