Reputation: 7252
This is follow up on another question that I had a while ago boost variant simple call to common methods.
Thanks to the suggested idea there I can do
boost::apply_visitor([](auto const& obj) { obj.some_operation(); }, variant);
I extended the idea and now I have:
auto visitor = [](auto const& element) { return element->do_something(); };
as a global variable and the visiting it is simply:
boost::apply_visitor(visitor, variant);
My question is how I can extend this pattern if possible to be able to use it for lambdas that accept additional arguments. Say if I have:
auto visitor = [](auto const& element, int a) { return element->do_something(a); };
boost::apply_visitor(<some visitor magic with passing a>, variant);
Note that at the time the lambda is defined there is nothing to capture there.
Upvotes: 1
Views: 807
Reputation: 217810
Note that at the time the lambda is defined there is nothing to capture there.
You may define it later, or define an other one at moment of the call:
auto visitor = [](auto const& element, int a) { return element->do_something(a); };
// ... with possible usage of visitor
const int b = ...;
boost::apply_visitor([&](auto const& element) {visitor(element, b);}, variant);
Upvotes: 1