TMP

Conventions

template <typename T> struct SizeOfT {
enum { value = sizeof(T) };
};
template <typename T> struct TPtr {
typedef T* type;
};

Template args can be types or numbers

template <size_t N> struct IsEven {
enum { value = (N %2 ) ? 0 : 1 };
};

Metafunctions

NOT tools of metaprogramming

constexpr

if then else

template <size_t N> struct IsPower2 {
enum { value = (N & (N - 1)) == 0 };
};

static_assert(IsPower2<N>::value, "N must be 2^n");
is_pointer
is_reference
has_member_function
std::integral_constant // wrap constant with its type

std::bool_constant // template alias

true_type and false_type are called nullary metafunctions because they have no params.

true_type::value
false_type::value

SFINAE

Substitution failure is not an error: this is where compiler errors go to hide.

Argument non-evaluation

constexpr

constexpr silently decays to not-compile time unless you use it in a context that needs to be known at compile time: within square brackets.

Definition

See CPP reference.

A template is a C++ entity that defines one of the following:

Metafunctions

Example traits

Cpp17UnaryTypeTrait

Cpp17UnaryTypeTrait

Cpp17TransformationTrait

Undefined behaviour

Type metafunctions

Explicit spectialisation

template <> // explicit speciailisation

Printing the type

std::cout << typeid(Type1).name() << 'n';

Compiler support

Now most compilers implement traits with intrinsics.

Primary type categories (traits)

All inherit from either true_type or false_type, and all should yield the same result in light of cv qualifiers.

For any given type T, exactle one of the primary type categories shall yield true_type.

is_void
is_null_pointer
is_integral
is_floating_point
is_array
etc

Equals combinatorial explosion!

So...

Metafunction abstractions

Function overload resolution

Comparisons with auto in C++20

These generate the same code:

template <typename T>
T func(T t) {
  return t;
}

auto func2(auto t) {
  return t;
}

See Jason Turner.

Other C++ standards

template<int N>
struct abs {
  static_assert (N != INT_MIN); // 2s comp
  static constexpr int value = (N < 0) ? -N : N; // initialise value not assign, takes place of return
};

// call
abs<n>value // instantiation yields a complile-time constant

Compile time recursion: metafunction that calls itself

template <unsigned M, unsigned N>
  struct gcd {
  static int const value = gcd<N, M%N>::value; // per Euclid
};

Suppply base case with a specialisation

template <unsigned M>
struct gcd<M, 0>{
  static_assert (M!=0);
  static int const value = M;
};

What's the relationship between constexpr functions and templates?

See also Walter E. Brown.

Identity metafunction

template <class T>
struct type_is {using type = T};
enable_if<false, ...>::type -- always an error? No! Welcome to SFINAE

value

void is one of the 15 types that C++ describes.

is_void<T>::value // since TR1
bool(is_void<T>{}) // instantiate/cast C++11
is_void<T>{}() // variable templates C++14
is_void_v<T> // value returning metafunctions C++17
using true_type = bool_constant<true>;
using false_type = bool_constant<false>;

Concepts

First parameter packs

template <class T, class... P0toN>
struct is_one_of; // declaration not implementation
// base cases
struct is_one_of<T> : false_type{};
struct is_one_of<T, T, P1toN...> : true_type{};
struct is_one_of<T, P0, P1toN>
: is_one_of<T, P1toN...>{};

Not evaluated

sizeof, typeid, decltype and noexcept are never evaluated, not even at compile time.

Examples

References