C++ Standard Library<type_traits>

<type_traits>Is a very useful header file in the C++ standard library, containing a set of tools for checking type traits at compile time. These tools can help developers determine type characteristics at compile time, thereby enabling safer and more flexible code.

<type_traits>The header file defines a set of templates that can be used to query and manipulate type traits. These traits include, but are not limited to:

  • Whether it is an integer type
  • Whether it is a floating point type
  • Whether it is a pointer type
  • Whether it is a reference type
  • whether it is callable (function or function pointer)

Syntax

<type_traits>The templates in it usually usestd::prefix, e.g.std::is_integral<T>::valueUsed to check typesTwhether it is an integer type. Herevalueis a static constant whose value istrueorfalse。

The following are some commonly usedtype_traitsFunction:

Basic type determination:

  • std::is_void<T>: Determine the typeTwhether it isvoid。
  • std::is_integral<T>: Determine the typeTWhether it is an integer type.
  • std::is_floating_point<T>: Determine the typeTWhether it is a floating point type.
  • std::is_array<T>: Determine the typeTwhether it is an array type.
  • std::is_pointer<T>: Determine the typeTwhether it is a pointer type.
  • std::is_reference<T>: Determine the typeTwhether it is a reference type.
  • std::is_const<T>: Determine the typeTwhether it isconstqualified.

Type modifiers:

  • std::remove_const<T>: Remove the typeTofconstqualified.
  • std::remove_volatile<T>: Remove the typeTofvolatilequalified.
  • std::remove_cv<T>: also remove the typeTofconstandvolatilequalified.
  • std::remove_reference<T>: Remove the typeT's reference qualifier.
  • std::remove_pointer<T>: Remove the typeT's pointer qualifier.

Type conversion:

  • std::add_const<T>: to the typeTaddconstqualified.
  • std::add_volatile<T>: to the typeTaddvolatilequalified.
  • std::add_cv<T>: Also, for the typeTaddconstandvolatilequalified.
  • std::add_pointer<T>: to the typeTadd a pointer qualifier.
  • std::add_lvalue_reference<T>: to the typeTadd an lvalue reference qualifier.
  • std::add_rvalue_reference<T>: to the typeTadd an rvalue reference qualifier.

Type trait detection:

  • std::is_same<T, U>: Determine the typeTandUwhether they are the same.
  • std::is_base_of<Base, Derived>: Determine the typeBasewhether it is the typeDerived's base class.
  • std::is_convertible<From, To>: Determine the typeFromwhether it can be converted to typeTo。

Conditional type:

  • std::conditional<Condition, T, F>: ifConditionistrue, then the type isT, otherwise it isF。
  • std::enable_if<Condition, T>: ifConditionistrue, then the type isT, otherwise this template does not participate in overload resolution.

Example

The following are some usage<type_traits>examples, and their output results.

check whether it is an integer type

Example

#include <iostream>
#include <type_traits>

int main() {
    std::cout << "int is integral: " << std::is_integral<int>::value << std::endl;
    std::cout << "float is integral: " << std::is_integral<float>::value << std::endl;
    std::cout << "char is integral: " << std::is_integral<char>::value << std::endl;
    return 0;
}

Output:

int is integral: 1
float is integral: 0
char is integral: 1

check whether it is a floating-point type

Example

#include <iostream>
#include <type_traits>

int main() {
    std::cout << "int is floating_point: " << std::is_floating_point<int>::value << std::endl;
    std::cout << "float is floating_point: " << std::is_floating_point<float>::value << std::endl;
    std::cout << "double is floating_point: " << std::is_floating_point<double>::value << std::endl;
    return 0;
}

Output:

int is floating_point: 0
float is floating_point: 1
double is floating_point: 1

check whether it is a pointer type

Example

#include <iostream>
#include <type_traits>

int main() {
    int a = 10;
    int* p = &a;
    std::cout << "int* is a pointer: " << std::is_pointer<int*>::value << std::endl;
    std::cout << "int is a pointer: " << std::is_pointer<int>::value << std::endl;
    return 0;
}

Output:

int* is a pointer: 1
int is a pointer: 0

check whether it is a reference type

Example

#include <iostream>
#include <type_traits>

int main() {
    int a = 10;
    int& ref = a;
    std::cout << "int& is a reference: " << std::is_reference<int&>::value << std::endl;
    std::cout << "int is a reference: " << std::is_reference<int>::value << std::endl;
    return 0;
}

Output:

int& is a reference: 1
int is a reference: 0

check whether it is callable

Example

#include <iostream>
#include <type_traits>
#include <functional>

void func() {}

int main() {
    std::cout << "int is callable: " << std::is_callable<int>::value << std::endl;
    std::cout << "void() is callable: " << std::is_callable<void()>::value << std::endl;
    std::cout << "func is callable: " << std::is_callable<decltype(func)>::value << std::endl;
    return 0;
}

Output:

int is callable: 0
void() is callable: 1
func is callable: 1

<type_traits>is a very useful tool in C++ that allows developers to check and manipulate type traits at compile time. This not only improves code safety, but also makes code more flexible and reusable.

other extensions