$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [Boost-users] switch_
From: niXman (i.nixman_at_[hidden])
Date: 2015-09-25 09:25:18
> I see Boost.Switch_ was provisionally accepted back inJan  2008.
> I see some doc here:
> http://dancinghacker.com/switch
> But I can't see it released in recent boost versions
> 
> I need to select a type to dynamic_cast to based on a runtime index, so 
> I
> would have the type in a mpl::vector, and use the switch_ to pick up 
> the
> right type? I have under 20 types.
> 
> in pseudo c++11:
> 
> 
> void f(std::size_t i, const T* obj)
> {
> 
> typedef mpl::vector<T0, .... Tn> alltypes_t;
> 
> const auto*  drvobj = dynamic_cast<...>(obj);
> /// what do I set as arg of dynamic_cast
> 
> }
> 
> If switch_ can help, where do I get the code for it?
Hi,
You can simply use the 'std::map<std::size_t, void(*)(const void*)>' 
like this:
void f(std::size_t i, const void* obj) {
    static const std::map<std::size_t, void(*)(const void*)> map = {
        {0 , [](const void *obj){ derived0  *p = dynamic_cast<derived0 
 >(o); ...; }}
       ,{1 , [](const void *obj){ derived1  *p = dynamic_cast<derived1 
 >(o); ...; }}
       ,{2 , [](const void *obj){ derived2  *p = dynamic_cast<derived2 
 >(o); ...; }}
       ,{18, [](const void *obj){ derived18 *p = 
dynamic_cast<derived18>(o); ...; }}
    };
    auto it = map.find(i);
    assert(it != map.end());
    it->second(obj);
}
...
> const auto*  drvobj = dynamic_cast<...>(obj);
> /// what do I set as arg of dynamic_cast
In that case the boost::switch_ not help you.