If all the types in my boost::variant support the same method, is there a way to call it generically (i.e. not calling it seperately for each method of the static_visitor)?
I’m trying to get something like this to work:
class A
{
void boo() {}
};
class B
{
void boo() {}
};
class C
{
void boo() {}
};
typedef boost::variant<A, B, C> X;
void foo(X& d)
{
x.boo();
}
but it fails to compile saying 'boo' : is not a member of 'boost::variant<T0_,T1,T2>'.
Currently, I have some classes all inherit from an interface so that their single shared method can be used polymorphically. I also want to be able to use the classes via a visitor as all other methods are unique to each concrete class. I was hoping boost::variant might be a better alternative to implementing my own visitor mechanism here. Is it?
There’s no direct way, but you can make the static_visitor pretty concise using templating.
Modified from the boost docs:
Now you can do this:
Infact you can generalise this to take a function pointer of your base class:
Then you can do:
Or something like that – I’ve probably got my function pointer syntax wrong, but hopefully you get the idea.