In the latest working draft (page 572) of the C++ standard the converting constructor of std::variant
is annotated with:
template co
The technique I'll describe is to actually build an overload set, and perform overload resolution by attempting to call it and see what happens with std::result_of
.
We define a function object that recursively defines an T operator()(T) const
for each T
.
template
struct identity { using type = T; };
template struct overload;
template <> struct overload<> { void operator()() const; };
template
struct overload : overload {
using overload::operator();
identity operator()(T) const;
};
// void is a valid variant alternative, but "T operator()(T)" is ill-formed
// when T is void
template
struct overload : overload {
using overload::operator();
identity operator()() const;
};
We can now use std::result_of_t
to simulate overload resolution, and find the winner.
// Find the best match out of `Ts...` with `T` as the argument.
template
using best_match = typename std::result_of_t(T)>::type;
Within variant
, we would use it like this:
template >
constexpr variant(T&&);
Alright! Are we done? The following tests pass!
// (1) `variant v("abc");` // OK
static_assert(
std::is_same_v>);
// (2) `variant w("abc");` // ill-formed
static_assert(
std::is_same_v>);
// (3) `variant x("abc");` // OK, but chooses bool
static_assert(
std::is_same_v>);
Well, we don't want (2)
to pass, actually. Let's explore a few more cases:
If there are no viable matches, the constructor simply SFINAEs out.
We get this behavior for free in best_match
, because std::result_of
is SFINAE-friendly as of C++14 :D
We want the best match to be a unique best match. This is (2)
that we would like to fail. For example, we can test this by checking that the result of best_match
appears exactly once in Ts...
.
template
constexpr size_t count() {
size_t result = 0;
constexpr bool matches[] = {std::is_same_v...};
for (bool match : matches) {
if (match) {
++result;
}
}
return result;
}
We can then augment this condition onto best_match
in a SFINAE-friendly way:
template
using best_match_impl = std::enable_if_t<(count() == 1), T>;
template
using best_match = best_match_impl(T)>, Ts...>;
(2)
now fails, and we can simply use best_match
like this:
template >
constexpr variant(T&&);
template print; // undefined
template
class variant {
template >
constexpr variant(T&&) {
print{}; // trigger implicit instantiation of undefined template error.
}
};
// error: implicit instantiation of undefined template
// 'print >'
variant v("abc");
// error: no matching constructor for initialization of
// 'variant'
variant w("abc");
// error: implicit instantiation of undefined template 'print'
variant x("abc");