2010-05-15 5 views
9
이것은 지금까지 내가받은 것 같다

,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; 
}; 

내가 이것을 어떻게 작성합니까? (나는 std :: for_each를 사용해야하는지 모르겠다. 여기에 다른 대답을 기반으로 한 추측이다.)

답변

13

유형 반복 실행 오버레이에는 mpl::for_each을 사용한다. 예 :

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

감사합니다. 래퍼 객체 대신 boost :: bind를 사용하는 방법이 있습니까? – Kyle

+2

@ 카일 : 나는 그렇게 생각하지 않는다 - 나는 Boost.Bind에서 유틸리티를 인식하지 못하기 때문에 템플릿으로 된'operator()'를 사용하여 필요한 펑터를 생성한다. –

+0

@ GeoFritzsche : do_this_wrapper를 람다로 만드는 방법이 있나요? (C++ 11/14/17 현재)? –

관련 문제