这就是我所得到的,
#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/