Metaprogramming library

From cppreference.com
< cpp
 
 
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)(deprecated in C++23)
(C++11)(deprecated in C++23)
(C++11)
(C++11)
(C++17)

(C++11)(until C++20)(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
 

C++ provides metaprogramming facilities, such as type traits, compile-time rational arithmetic, and compile-time integer sequences.

Type property

These type traits define compile-time template-based interfaces to query the properties of types.

Attempting to specialize a template defined in the <type_traits> header and described in this section results in undefined behavior.

A template defined in the <type_traits> header may be instantiated with an incomplete type unless otherwise specified, notwithstanding the general prohibition against instantiating standard library templates with incomplete types.

Defined in header <type_traits>
Primary type categories
(C++11)
checks if a type is void
(class template)
checks if a type is std::nullptr_t
(class template)
checks if a type is an integral type
(class template)
checks if a type is a floating-point type
(class template)
(C++11)
checks if a type is an array type
(class template)
(C++11)
checks if a type is an enumeration type
(class template)
(C++11)
checks if a type is an union type
(class template)
(C++11)
checks if a type is a non-union class type
(class template)
checks if a type is a function type
(class template)
checks if a type is a pointer type
(class template)
checks if a type is a lvalue reference
(class template)
checks if a type is a rvalue reference
(class template)
checks if a type is a pointer to a non-static member object
(class template)
checks if a type is a pointer to a non-static member function
(class template)
Composite type categories
checks if a type is a fundamental type
(class template)
checks if a type is an arithmetic type
(class template)
(C++11)
checks if a type is a scalar type
(class template)
(C++11)
checks if a type is an object type
(class template)
checks if a type is a compound type
(class template)
checks if a type is either a lvalue reference or rvalue reference
(class template)
checks if a type is a pointer to an non-static member function or object
(class template)
Type properties
(C++11)
checks if a type is const-qualified
(class template)
checks if a type is volatile-qualified
(class template)
checks if a type is trivial
(class template)
checks if a type is trivially copyable
(class template)
checks if a type is a standard-layout type
(class template)
(C++11)(deprecated in C++20)
checks if a type is a plain-old data (POD) type
(class template)
(C++11)(deprecated in C++17)(removed in C++20)
checks if a type is a literal type
(class template)
checks if every bit in the type's object representation contributes to its value
(class template)
(C++11)
checks if a type is a class (but not union) type and has no non-static data members
(class template)
checks if a type is a polymorphic class type
(class template)
checks if a type is an abstract class type
(class template)
(C++14)
checks if a type is a final class type
(class template)
checks if a type is an aggregate type
(class template)
(C++11)
checks if a type is a signed arithmetic type
(class template)
checks if a type is an unsigned arithmetic type
(class template)
checks if a type is an array type of known bound
(class template)
checks if a type is an array type of unknown bound
(class template)
checks if a type is a scoped enumeration type
(class template)
Supported operations
checks if a type has a constructor for specific arguments
(class template)
checks if a type has a default constructor
(class template)
checks if a type has a copy constructor
(class template)
checks if a type can be constructed from an rvalue reference
(class template)
checks if a type has a assignment operator for a specific argument
(class template)
checks if a type has a copy assignment operator
(class template)
checks if a type has a move assignment operator
(class template)
checks if a type has a non-deleted destructor
(class template)
checks if a type has a virtual destructor
(class template)
checks if objects of a type can be swapped with objects of same or different type
(class template)
Property queries
obtains the type's alignment requirements
(class template)
(C++11)
obtains the number of dimensions of an array type
(class template)
(C++11)
obtains the size of an array type along a specified dimension
(class template)
Type relationships
(C++11)
checks if two types are the same
(class template)
checks if a type is derived from the other type
(class template)
checks if a type can be converted to the other type
(class template)
checks if a type can be invoked (as if by std::invoke) with the given argument types
(class template)
checks if a reference is bound to a temporary in direct-initialization
(class template)
checks if a reference is bound to a temporary in copy-initialization
(class template)
checks if two types are layout-compatible
(class template)
checks if a type is a pointer-interconvertible (initial) base of another type
(class template)
checks if objects of a type are pointer-interconvertible with the specified subobject of that type
(function template)
checks if two specified members correspond to each other in the common initial subsequence of two specified types
(function template)

Operations on traits

Defined in header <type_traits>
variadic logical AND metafunction
(class template)
variadic logical OR metafunction
(class template)
(C++17)
logical NOT metafunction
(class template)

Base classes

Defined in header <type_traits>
compile-time constant of specified type with specified value
(class template)

Two specializations of std::integral_constant for the type bool are provided:

Defined in header <type_traits>
Specializations
Type Definition
true_type std::integral_constant<bool, true>
false_type std::integral_constant<bool, false>

Type modifications

These type traits apply modifications on a template parameter, and declare (sometimes conditionally) the type member typedef as the resulting type.

Attempting to specialize a template defined in the <type_traits> header and described in this section results in undefined behavior, except that std::common_type and std::basic_common_reference (since C++20) may be specialized as required in description.

A template defined in the <type_traits> header may be instantiated with an incomplete type unless otherwise specified, notwithstanding the general prohibition against instantiating standard library templates with incomplete types.

Defined in header <type_traits>
Const-volatility specifiers
removes const or/and volatile specifiers from the given type
(class template)
(C++11)(C++11)(C++11)
adds const or/and volatile specifiers to the given type
(class template)
References
removes a reference from the given type
(class template)
adds a lvalue or rvalue reference to the given type
(class template)
Pointers
removes a pointer from the given type
(class template)
adds a pointer to the given type
(class template)
Sign modifiers
makes the given integral type signed
(class template)
makes the given integral type unsigned
(class template)
Arrays
removes one extent from the given array type
(class template)
removes all extents from the given array type
(class template)

Miscellaneous transformations

Defined in header <type_traits>
(C++11)(deprecated in C++23)
defines the type suitable for use as uninitialized storage for types of given size
(class template)
(C++11)(deprecated in C++23)
defines the type suitable for use as uninitialized storage for all given types
(class template)
(C++11)
applies type transformations as when passing a function argument by value
(class template)
combines std::remove_cv and std::remove_reference
(class template)
(C++11)
conditionally removes a function overload or template specialization from overload resolution
(class template)
chooses one type or another based on compile-time boolean
(class template)
determines the common type of a group of types
(class template)
determines the common reference type of a group of types
(class template)
obtains the underlying integer type for a given enumeration type
(class template)
(C++11)(removed in C++20)(C++17)
deduces the result type of invoking a callable object with a set of arguments
(class template)
(C++17)
void variadic alias template
(alias template)
returns the type argument unchanged
(class template)

Compile-time rational arithmetic

The header <ratio> provides types and functions for manipulating and storing compile-time ratios.

Compile-time integer sequences

Defined in header <utility>
implements compile-time sequence of integers
(class template)