Boost logo

Boost Users :

From: Duane Murphy (duanemurphy_at_[hidden])
Date: 2005-10-05 18:51:45


--- At Wed, 5 Oct 2005 09:50:54 -0400, David Abrahams wrote:

>Aleksey Gurtovoy <agurtovoy_at_[hidden]> writes:
>
>>> I think you can see why I'd like a type list iterator solution to this.
>>
>> Try this:
>>
>> struct do_something
>> {
>> do_something( BASE* base ) : m_base( base ) {}
>>
>> template< typename T > void operator()( mpl::identity<T> ) const
>> {
>> if ( T* x = dynamic_cast< T* >( this->m_base ) )
>> some_function( x );
>> }
>>
>> BASE* m_base;
>> };
>>
>> mpl::for_each<
>> mpl::vector<A,B,C>
>> , mpl::make_identity<_1>
>> >( do_something( base ) );
>>
>
>In this case you test for all the derived types, even if you match the
>first one, so it's not identical. It would probably be nice of us to
>create a for_each_if() or until() function that stops when the
>function object returns true. I'm pretty sure I've needed something
>like that before... Oh yeah, the FSM example could probably benefit
>from it! A lot!

Thanks for this solution Aleksey! This works great. I also used your
suggestion to throw an exception to unwind when the right type was
found. (Throwing an exception is not my favorite solution, but functional.)

David, I'm only working with the boost documentation; I don't have the
book, so I could not understand your suggestion using fold<>. It didn't
seem to match the template.

typedef fold<sequence,state,op>::type t;

I further didn't understand the NextFunctionType argument. Maybe the
book would make this more clear. :-)

Not having documentation for for_each makes it difficult to use. ;-) All
of the documentation that I found, talks about meta-functions. for_each
() seems to be a real function. This is quite useful to understand. What
other real functions are undocumented?

Thanks again for the help and the education!

 ...Duane


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