Generic in cpp.

How to Deal with Ambiguity?

  • 1
auto [min, max] = my_minimax(std::string("A"), std::string("C-String"));
  • 2
auto [min, max] = my_minimax<std::string>("A", "C-String");

Concept Lifting

Example: Why Integer? It can’t be something else?
Let’s relax this constraint!

Implicit Interface and Concept

A template function defines an implicit interface that each template parameter must satisfy. concept:

template <typename C, typename T>
concept IndexableContainer = requires(C c, size_t i, T val) {
    { c.size() } -> std::convertible_to<size_t>;
    { c[i] } -> std::convertible_to<T>;
    { c[i] == val } -> std::convertible_to<bool>;
};

We can make it more generalized by Lambda and Function!

Overload Resolution

Varadic templates