|
Boost Users : |
Subject: Re: [Boost-users] [variant] Variant call forwarding?
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2010-02-23 14:31:30
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);
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net