2010-05-15 15 views
9

Questo è quanto ho ottenuto,Come eseguire il looping di un boost :: mpl :: list?

#include <boost/mpl/list.hpp> 
#include <algorithm> 
namespace mpl = boost::mpl; 

class RunAround {}; 
class HopUpAndDown {}; 
class Sleep {}; 

template<typename Instructions> int doThis(); 
template<> int doThis<RunAround>() { /* run run run.. */ return 3; } 
template<> int doThis<HopUpAndDown>() { /* hop hop hop.. */ return 2; } 
template<> int doThis<Sleep>()  { /* zzz.. */ return -2; } 


int main() 
{ 
    typedef mpl::list<RunAround, HopUpAndDown, Sleep> acts; 

// std::for_each(mpl::begin<acts>::type, mpl::end<acts>::type, doThis<????>); 

    return 0; 
}; 

Come faccio a completare questo? (Non so se dovrei usare std :: for_each, solo un'ipotesi basata su un'altra risposta qui)

risposta

13

Utilizzare mpl::for_each per l'iterazione di runtime su liste di tipi. Es .:

struct do_this_wrapper { 
    template<typename U> void operator()(U) { 
     doThis<U>(); 
    } 
}; 

int main() { 
    typedef boost::mpl::list<RunAround, HopUpAndDown, Sleep> acts; 
    boost::mpl::for_each<acts>(do_this_wrapper());  
}; 
+1

Grazie - c'è un modo per farlo utilizzando boost :: bind invece dell'oggetto wrapper? – Kyle

+2

@Kyle: io non la penso così - non sono a conoscenza di alcuna utilità in Boost.Bind che genera i necessari funtori con un operatore di template() '. –

+0

@GeorgFritzsche: C'è un modo per fare 'do_this_wrapper' a lambda (come in C++ 11/14/17)? –

Problemi correlati