如何通过一个boost :: MPL ::列表循环? [英] How to loop through a boost::mpl::list?

查看:388
本文介绍了如何通过一个boost :: MPL ::列表循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是据我已经得到了,

#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的,只是猜测)

How do I complete this? (I don't know if I should be using std::for_each, just a guess based on another answer here)

推荐答案

使用的 MPL :: for_each的运行时遍历所有类型的列表。例如:

Use mpl::for_each for runtime iteration over type lists. E.g.:

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());    
};

这篇关于如何通过一个boost :: MPL ::列表循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆