c++ - 如何遍历 boost::mpl::list?

标签 c++ templates boost metaprogramming 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,只是基于此处另一个答案的猜测)

最佳答案

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

关于c++ - 如何遍历 boost::mpl::list?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2840640/

相关文章:

c++ - boost ASIO : Send message to all connected clients

c++ - 当 boost 内存映射文件分配的磁盘空间多于 HDD 上的可用空间时,如何检测错误

c++ - 使用静态局部变量获取绑定(bind)/推断捕获 lambda 的函数指针

C++ - 如何为类模板声明函数模板友元

c++ - map 复杂查找操作

c++ - 为什么在通过模板进行静态调度时不需要前向声明?

c++ - 在模板中获取非常量类型

c++ - 如何在运行时检查 void* 是否为 IUnknown*?

c++ - 推导元组的类型

c++ - 在 gcc 4.4.7 Red Hat 6.6 上运行 gcc 5.2 build