Type_traits

The type_traits header file contains metaprogramming tools for compile-time type introspection. The header provides a set of templates that can be used to query properties of types.

  • has_push_back

template<typename T>
bool has_push_back_v = has_push_back<T>::value

Helper variable template to check if a type has a push_back method.

This variable template is a convenient way to check if a type T has a push_back method. It is equivalent to has_push_back<T>::value.

Template Parameters:

T – The type to check.

template<typename T, typename = void>
struct has_push_back : public std::false_type
#include <type_traits.hpp>

Trait to check if a type has a push_back method.

This primary template inherits from std::false_type, indicating that the type T does not have a push_back method.

Template Parameters:
  • T – The type to check.

  • Ignored – A parameter to enable SFINAE, defaults to void.

template<typename T>
struct has_push_back<T, std::void_t<decltype(std::declval<T>().push_back({}))>> : public std::true_type
#include <type_traits.hpp>

Specialization of has_push_back for types that have a push_back method.

This specialization inherits from std::true_type, indicating that the type T has a push_back method.

Template Parameters:

T – The type to check.

  • has_insert

template<typename T>
bool has_insert_v = has_insert<T>::value

Helper variable template to check if a type has an insert method.

This variable template is a convenient way to check if a type T has an insert method. It is equivalent to has_insert<T>::value.

Template Parameters:

T – The type to check.

template<typename T, typename = void>
struct has_insert : public std::false_type
#include <type_traits.hpp>

Trait to check if a type has an insert method.

This primary template inherits from std::false_type, indicating that the type T does not have an insert method.

Template Parameters:
  • T – The type to check.

  • Ignored – A parameter to enable SFINAE, defaults to void.

template<typename T>
struct has_insert<T, std::void_t<decltype(std::declval<T>().insert({}, {}))>> : public std::true_type
#include <type_traits.hpp>

Specialization of has_insert for types that have an insert method.

This specialization inherits from std::true_type, indicating that the type T has an insert method.

Template Parameters:

T – The type to check.

  • remove_cvref

template<typename T>
using remove_cvref = std::remove_cv<std::remove_reference_t<T>>

Alias template to remove const, volatile, and reference qualifiers from a type.

This alias template removes const, volatile, and reference qualifiers from the type T.

Template Parameters:

T – The type to remove qualifiers from.

template<typename T>
using remove_cvref_t = typename remove_cvref<T>::type

Alias template to remove const, volatile, and reference qualifiers from a type.

This alias template removes const, volatile, and reference qualifiers from the type T. It is equivalent to remove_cvref<T>::type.

Template Parameters:

T – The type to remove qualifiers from.

  • invoke_or_return

template<typename T, typename ...Funcs, std::size_t N = sizeof...(Funcs)>
std::enable_if_t<N != 0, T> invoke_or_return(T &arg, Funcs&&... funcs)

Invokes a series of functions on a copy of the given argument and returns the modified copy.

This function template takes an argument by reference, creates a copy of it, and then invokes each of the provided functions on the copy. The modified copy is then returned.

Note

The signature of the functions must be equivalent to void(T &a).

Template Parameters:
  • T – The type of the argument.

  • Funcs – The types of the functions to be invoked.

  • N – The number of functions to be invoked (deduced automatically).

Parameters:
  • arg – The argument to be copied and modified.

  • funcs – The functions to be invoked on the copied argument.

Returns:

The modified copy of the argument.

Usage

The following examples demonstrates how to use the type_traits header file:

  • has_push_back

std::cout << std::boolalpha
          << "utils::has_push_back_v<int>: "
          << utils::has_push_back_v<int> << '\n'
          << "utils::has_push_back_v<std::vector<int>>: "
          << utils::has_push_back_v<std::vector<int>> << std::endl;

Output:

utils::has_push_back_v<int>: false
utils::has_push_back_v<std::vector<int>>: true
  • has_insert

std::cout << std::boolalpha
          << "utils::has_insert_v<int>: "
          << utils::has_insert_v<int> << '\n'
          << "utils::has_insert_v<std::vector<int>>: "
          << utils::has_insert_v<std::vector<int>> << std::endl;

Output:

utils::has_insert_v<int>: false
utils::has_insert_v<std::vector<int>>: true
  • remove_cvref

std::cout << std::boolalpha
          << "std::is_same_v<utils::remove_cvref_t<const int&>, int>: "
          << std::is_same_v<utils::remove_cvref_t<const int&>, int> << '\n'
          << "std::is_same_v<utils::remove_cvref_t<volatile int&>, int>: "
          << std::is_same_v<utils::remove_cvref_t<volatile int&>, int> << std::endl;

Output:

std::is_same_v<utils::remove_cvref_t<const int&>, int>: true
std::is_same_v<utils::remove_cvref_t<volatile int&>, int>: true
  • invoke_or_return

int some_function(SomeLargeObject& a, Funcs&&... funcs) {
  // If no funcs are provided, return the reference to the object itself
  // Otherwise, create a copy of object and apply the functions on it and return the result
    further_processing(a, utils::invoke_or_return(std::forward<Funcs>(funcs), a));
}