Is there any inexpensive way to pass an arguments of non-variant types in addition to arguments of variant types, when multivisitor applyed?
What I mean by the term "expensive way" is:
#include <boost/variant.hpp>
#include <iostream>
#include <cstdlib>
struct A {};
struct B {};
enum class C { X, Y };
std::ostream &
operator << (std::ostream & out, C const c)
{
switch (c) {
case C::X : {
return out << "C::X";
}
case C::Y : {
return out << "C::Y";
}
default : {
break;
}
}
throw std::runtime_error("unknown C value");
}
using V = boost::variant< A, B >;
struct S
: boost::static_visitor<>
{
void
operator () (C const c, A const &) const
{
std::cout << c << " A" << std::endl;
}
void
operator () (C const c, B const &) const
{
std::cout << c << " B" << std::endl;
}
};
int main()
{
V const a = A{};
V const b = B{};
using VC = boost::variant< C >;
VC const x = C::X;
VC const y = C::Y;
S const s;
boost::apply_visitor(s, x, a);
boost::apply_visitor(s, y, a);
boost::apply_visitor(s, x, b);
boost::apply_visitor(s, y, b);
return EXIT_SUCCESS;
}
Another expensive way is to make non-static visitor with fileds of required types (or references to required types) and construct instances of such visitor for each set of values of non-variant types every time.