增强MPL嵌套lambda
Boost MPL nested lambdas
我一直在努力掌握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;
感谢Luc Touraille对我的问题的评论,Boost邮件列表提供了答案。下面的代码是:
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
。这足以绑定占位符的范围。
相关文章:
- Lambda 捕获、初始值设定项和嵌套结构
- 在嵌套 lambda 的情况下如何初始化 lambda 捕获?
- 是否可以指定 C++20 个模板化 lambda 来推断嵌套在参数中的类型?
- 修改嵌套 lambda 中捕获的参数:gcc 与 clang?
- 重构嵌套绑定以使用 lambda 函数
- 通过嵌套的lambda中的值捕获指针导致Segfault
- 为什么这个嵌套的 lambda 不被认为是 constexpr?
- 嵌套的 lambda 函数
- 如何更新嵌套lambda功能(C )中的变量
- 嵌套模板功能中的C 0x lambda参数
- 嵌套C lambda功能的标题
- 嵌套的lambda表达式在用Visual C++编译和生成巨大的对象文件时速度非常慢
- 命名嵌套类时无法解析 lambda 中的名称
- boost::bind in boost::lambda 的嵌套用法不起作用
- 嵌套Lambda捕获问题
- 如何从嵌套的 lambda 表达式函子类创建
- 使用lambda执行嵌套函数
- c++11嵌套lambda编译segfault
- (C++) 使用 lambda 作为嵌套函数,甚至用作通用函数
- 当在嵌套的lambda中使用“decltype”时,GCC的分离错误