Boost logo

Boost Users :

Subject: Re: [Boost-users] [variant] Variant call forwarding?
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2010-02-23 14:14:55


On Tue, Feb 23, 2010 at 11:59 AM, Igor R <boost.lists_at_[hidden]> wrote:

> > To be able to choose one of them at runtime :-) ... I'm looking for
> something similar to boost::function<>, but which doesn't erase all type
> information.
>
> Lets assume you've got 2 different functions with the same signature:
> void f1(int i, double d);
> void f2(int i, double d);
>
> What do you loose by putting them into boost::function<void(int, double)>?

You lose the ability

a) To use a function *object* which contains state
b) To use different pre & post call logic depending on which function is
selected.

Using a variant, you can imagine it ultimately boiling down to a large
"behind the scenes" if-then-else block that selects which object is to be
invoked. However, instead of simply doing the invoke, variant gives you the
flexibility of specifying arbitrary code to run both before and after the
invocation.

Also, boost::function<> has quite a high overhead compared to variant.

Zach



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