|
Boost : |
Subject: Re: [boost] [Interest] Value Dispatch
From: Larry Evans (cppljevans_at_[hidden])
Date: 2012-08-15 14:06:49
On 08/15/12 08:49, *smgreened_at_[hidden] wrote:
> Hi all,
>
> This might be old hat to verteran boosters but I found it nifty. Maybe
> it could make a small addition to Boost.
>
> Inspired by Sean Parent's C++Now! talk and the various articles on
> cpp-next, I've been playing around with value semantics and C++11.
>
> I found an interesting use of pack expansion at stackoverflow and
> expanded on it in a local project to convert from a traditional
> inheritance/virtual function model to a more value-centered approach.
>
> It turns out that with pack expansion one can implement "virtual
> functions" without needing inheritance or pointer/reference semantics.
> One can consider a vrtual function as "just" a compiler-generated switch
> statement. With that in mind, here's an example:
>
> -----------
[snip]
> class vehicle {
> public:
> enum kind {
> Car,
> Bus,
> Boat,
> Train,
> Airplane
> };
>
> typedef kind_tuple<vehicle,
> kind::Car, kind::Bus, kind::Boat, kind::Train,
> kind::Airplane> kinds;
>
> private:
[snip]
> kind the_kind;
>
> public:
> vehicle(kind k) : the_kind(k) {}
[snip]
>
> };
[snip]
Could you not use boost::variant, where the variant which function would
return the tag, which is what the above vehicle::the_kind is,
in essence:
------------
#include <boost/variant.hpp>
enum kind {
Car,
Bus,
Boat,
Train,
Airplane
};
template
< kind Kind
>
class vehicle_kind
{
};
typedef
boost::variant
< vehicle_kind<Car>
, vehicle_kind<Bus>
, vehicle_kind<Boat>
, vehicle_kind<Train>
, vehicle_kind<Airplane>
>
vehicle
;
------------
Then couldn't boost::variant library's static_visitor's do the
dispatching?
-regards,
Larry
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk