...

/

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 ...