Chapter 37. Constructing visitors for std::variants


Checking if a type is visited by a variant
#include <x/visitor.H>

struct one{};
struct two{};

void foo(const std::variant<one, two> &bar)
        std::visit( x::visitor{
            [](const one &o) {},
            [](const two &t) {}
        }, bar);

The x::visitor template constructs a callable object for passing to std::visit. The intended usage is to pass a variadic list of lambdas to the template, using uniform initialization syntax. This constructs a callable object that inherits all passed lambas, which then gets passed to std::visit.

Checking if a type is visited by a variant

typedef std::variant<int, bool> v;

template<typename T, typename U=std::enable_if_<x::w::visited<T, v>::value>>
struct x{};

x<int> a;            // This compiles

x<const char *>b;    // Compilation error.

x::w::visited<T, V>::value indicates whether type T is one of the types in V which must be a std::variant.

x::w::visited_v<T, V> is an alias for this value.