提升MPL嵌套lambda

时间:2013-04-18 16:02:13

标签: c++ boost-mpl

我一直试图掌握Boost MPL

作为简单的练习,我尝试过:

typedef vector_c<int, 1, 2, 3, 4, 5>::type example_list;

typedef transform<example_list, times<_, int_<2> > >::type doubled_example_list;

typedef transform<example_list, negate<_> >::type negated_example_list;

BOOST_STATIC_ASSERT((at_c<negated_example_list, 2>::type::value==-3));
BOOST_STATIC_ASSERT((at_c<doubled_example_list, 4>::type::value==10));

这些都很好。但是,以下尝试无法编译:

typedef transform<_, negate<_> > negate_a_list;

typedef apply<negate_a_list, example_list>::type negated_example_list_2;

BOOST_STATIC_ASSERT((at_c<negated_example_list_2, 2>::type::value==-3));

我认为这与negate_a_list中占位符的范围有关,但我不确定如何修复它。有任何想法吗?我还怀疑我对MPL语法和语义的一些假设是有缺陷的。我很感激任何关于修行MPL的提示。

P.S。以下是上述代码的序言:

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/static_assert.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/times.hpp>
#include <boost/mpl/size_t.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/lambda.hpp>
#include <boost/mpl/negate.hpp>
#include <boost/mpl/at.hpp>

using namespace boost::mpl;
using namespace boost::mpl::placeholders;

1 个答案:

答案 0 :(得分:5)

感谢Luc Touraille对我的问题的评论,Boost邮件列表提供了the answer。此代码有效:

typedef transform<_, lambda<negate<_> >::type > negate_a_list;

typedef apply<negate_a_list, example_list>::type negated_example_list_2;

BOOST_STATIC_ASSERT((at_c<negated_example_list_2, 2>::type::value==-3));

请注意在lambda表达式周围添加lambda<...>::type包装。这足以限制占位符的范围。

相关问题