I’m trying to use boost tuples to avoid some virtual function overhead, and I can’t quite make it work. I have a vector of “handlers” that try to handle an input, and once one of them returns true, I don’t want to call the rest of them.
C++11 is not available.
First, the current virtual implementation looks like this:
std::vector<Handler*> handlers;
//initialization code...
handlers.push_back(new Handler1);
handlers.push_back(new Handler2);
handlers.push_back(new Handler3);
//runtime code
for(std::vector<Handler*>::iterator iter = handlers.begin();
iter != handlers.end() && !(*iter)->handle(x); ++iter) {}
Since I have all the types at compiletime, I’d prefer to be able to express this as a tuple, like this:
boost::tuple<Handler1,Handler2,Handler3> handlers;
//runtime code
???
// should compile to something equivalent to:
// if(!handlers.get<0>().handle(x))
// if(!handlers.get<1>().handle(x))
// handlers.get<2>().handle(x);
Ideally there would be no virtual function calls and any empty function bodies would inline out. It seems like this might almost be possible with boost::fusion for_each, but I need the short circuiting behavior where once one of the handlers returns true, the rest of them are not called.
You could try something like this:
To use just pass your tuple to
my_call.