
Rutger ter Borg a écrit :
Hello,
I'm wondering what's the easiest way to enable call forwarding by a variant to contained function objects accepting the same arguments?
I.e.,
variant< F, G > m_funcs;
m_funcs = f; m_funcs( a, b, c );
m_funcs = g; m_funcs( a, b, c );
Let's consider that's the signature is R (T1, T2, T3). struct call_visitor : static_visitor<R> { call_visitor(T1&& a_, T2&& b_, T3&& c_) : a(a_), b(b_), c(c_) { } template<typename F> R operator()(F& f) const { return f(a, b, c); } T1&& a; T2&& b; T3&& c; }; Then you could do what you want with apply_visitor(call_visitor(a, b, c), m_funcs); If you want to do it for arbitrary signatures and provide perfect forwarding without rvalue references, it is going to be a bit more work. With polymorphic lambdas (an extension to C++0x lambdas), it is quite simpler: apply_visitor([&](auto& f) { return f(a, b, c); }, m_funcs);