EDIT: Boost now supports multi-visitation, as does C++17 variant.
If you have a variant type with a unary visit
member function, this can be extended to an n-ary-apply_visitor
function as follows:
Include the necessary standard library dependencies:
#include <tuple>
#include <type_traits>
#include <utility> //For C++14 `std::integer_sequence`.
//If you don't want to use C++14, write your own.
Now a helper function for making a new tuple identical to an existing tuple but without the first element:
template<std::size_t ...S, typename Head, typename ...Tail>
std::tuple<Tail...> tuple_tail_impl(
index_sequence<S...>,
std::tuple<Head, Tail...> const &in_tuple)
{
struct In {
template<std::size_t N>
using ElementType =
typename std::tuple_element<N, std::tuple<Head, Tail...>>::type;
};
return std::tuple<Tail...>(
std::forward<In::ElementType<S+1>>(std::get<S+1>(in_tuple))...);
}
template<typename Head, typename ...Tail>
std::tuple<Tail...> tuple_tail(std::tuple<Head, Tail...> const& in_tuple) {
return tuple_tail_impl(index_sequence_for<Tail...>(), in_tuple);
}
Now, the class that does the work, and a helper function for creating that class:
template<typename Visitor, typename MatchedValueTuple, typename... TailVariants>
struct NAryVisitorFlattener;
template<typename Visitor, typename MatchedValueTuple, typename... TailVariants>
NAryVisitorFlattener<Visitor, MatchedValueTuple, TailVariants...>
make_NAryVisitorFlattener(
Visitor &&visitor,
MatchedValueTuple &&matchedValues,
std::tuple<TailVariants...> &&tailVariants);
In the recursive case, NAryVisitorFlattener
sequentially calls the apply
member function on every variant and builds up the resulting values in MatchedValueTuple
.
template<
typename Visitor,
typename MatchedValueTuple,
typename CurrentVariant,
typename... TailVariants>
struct NAryVisitorFlattener<
Visitor, MatchedValueTuple, CurrentVariant, TailVariants...>
{
typedef typename
std::remove_reference<Visitor>::type::result_type result_type;
Visitor visitor;
MatchedValueTuple matchedValues;
std::tuple<CurrentVariant, TailVariants...> tailVariants;
template<typename A>
result_type operator()(A &&a)
{
auto flattener = make_NAryVisitorFlattener(
std::forward<Visitor>(visitor),
std::tuple_cat(matchedValues, std::forward_as_tuple(std::forward<A>(a))),
tuple_tail(tailVariants));
return std::forward<CurrentVariant>(std::get<0>(tailVariants))
.visit(flattener);
}
};
In the base-case, apply
has been called on every variant, and the visitor is called with the values in MatchedValueTuple
:
template<typename Visitor, typename MatchedValueTuple>
struct NAryVisitorFlattener<Visitor, MatchedValueTuple> {
typedef typename
std::remove_reference<Visitor>::type::result_type result_type;
Visitor visitor;
MatchedValueTuple matchedValues;
std::tuple<> tailVariants;
template<typename A>
result_type operator()(A &&a) {
return callFunc(
std::make_index_sequence<std::tuple_size<MatchedValueTuple>::value>(),
std::forward<A>(a));
}
template<std::size_t N>
using MatchedValueType =
typename std::tuple_element<N,MatchedValueTuple>::type;
template<std::size_t ...S, typename A>
result_type callFunc(std::index_sequence<S...>, A &&a) {
return std::forward<Visitor>(visitor)(
std::forward<MatchedValueType<S>>(matchedValues))...,
std::forward<A>(a));
}
};
And the definition of the helper function declared earlier:
template<typename Visitor, typename MatchedValueTuple, typename... TailVariants>
NAryVisitorFlattener<Visitor, MatchedValueTuple, TailVariants...>
make_NAryVisitorFlattener(
Visitor &&visitor,
MatchedValueTuple &&matchedValues,
std::tuple<TailVariants...> &&tailVariants)
{
return {
std::forward<Visitor>(visitor),
std::forward<MatchedValueTuple>(matchedValues),
std::forward<std::tuple<TailVariants...>>(tailVariants)
};
}
Now, the function you've been waiting for. Get the ball rolling with the first NAryVisitorFlattener
:
template<typename Visitor, typename VariantA, typename... Variants>
typename std::remove_reference<Visitor>::type::result_type
apply_visitor(Visitor &&visitor, VariantA &&variantA, Variants &&...variants) {
auto flattener = make_NAryVisitorFlattener(
std::forward<Visitor>(visitor),
std::tuple<>{},
std::forward_as_tuple(std::forward<Variants>(variants)...));
return std::forward<VariantA>(variantA).visit(flattener);
}
This is all taken from my complete C++11-compatible variant implementation available here.
boost::any
, ifboost::variant
is too constricting. Maybe combine those two even... – Bringapply_visitor(visitor, v0, v1, v2, ...)
– Clatterboost::variant
doesn't do and cannot do static dispatch. It is an illusion.boost::variant
implements a tagged union and dispatches on the tag. – Newcomer