我一直在努力掌握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的建议。
附注:以下是上述代码的前言:
#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;发布于 2013-04-19 00:31:39
感谢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。这足以限制占位符的范围。
https://stackoverflow.com/questions/16087806
复制相似问题