Type Traits (C++)

Allows us to query or modify properties of types at compile-time. Introduced in C++11.

Resources

Commonly used

  • std::enable_if
  • std::conditional
  • std::is_same
  • `std::remove_const
    • Removes const qualifier from T
    • difference with const_cast? That does it on a particular variable instance, as opposed to making a modification on a type
  • std::add_pointer<T>

Lots of modern C++ is around this.

#include <type_traits>
#include <iostream>
 
template <typename T>
void print(const T& x) {
    if constexpr (std::is_integral_v<T>) {
        std::cout << "int-like: " << x << "\n";
    } else {
        std::cout << "other: " << x << "\n";
    }
}