使用c++11占位符作为lambda
using c++11 placeholders as lambdas?
在试验c++ 11的新特性时,我发现std::placeholders::_1不能直接用作lambdas:
#include <algorithm>
#include <functional>
// #include <boost/lambda/lambda.hpp>
using namespace std;
// using boost::lambda::_1;
using std::placeholders::_1;
int main()
{
int a[] = {1,2,3,4,5};
transform(a, a+5, a, _1 * 2);
}
Clang 3.3错误:
tmp $ clang -std=c++11 -stdlib=libc++ -lc++ test.cpp
test.cpp:16:27: error: invalid operands to binary expression ('__ph<1>' and 'int')
transform(a, a+5, a, _1 * 2);
如果我把它改为使用Boost的版本,它编译得很好。
为什么这个不能在标准版本中工作?有办法让它工作吗,还是我必须在这里使用一个丑陋的lambda ?
transform(a, a+5, a, [](int i){return i*2;});
Boost实际上有许多_1
占位符。那是Boost的。Bind(或多或少被合并到c++ 11中),这些来自Boost。Lambda,甚至是Lambda的继任者Boost.Phoenix。
Lambda和Phoenix版本是唯一的占位符,可以用来自己创建函函数。的提振。Bind _1
占位符不能,这就是标准化的原因。Lambda和Phoenix是将表达式转换为函数的方法;Bind只是一个函数绑定和参数调整系统。
相关文章:
- 将模板与类模板扣除占位符参数匹配
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- Tensorflow C++ 占位符初始化
- std::bind 不会接受 bind 占位符的 std::cref - 为什么?
- qt 中的占位符
- 我可以在 c++ 中使用占位符作为数组的索引吗?
- 为什么协程不允许使用占位符返回类型?
- TensorFlow类占位符以及其他定义输入张量的方法
- QtLineEdit 占位符:如何让用户在插入模式下将字符 1 替换为 1
- 非类型模板参数中的占位符类型是否涉及作为模板参数传递的函数的重载解析?
- 是否可以使用自动占位符来推断非类型模板参数中的函数结果
- 如何使用 msgfmt 验证格式字符串中的位置表示法占位符
- 无法推断出占位符的概念
- 编译器错误boost :: asio ::占位符:: boost v1.69的错误
- std::绑定可变参数模板、绑定参数和占位符
- 尾随返回类型中的占位符类型的用途是什么
- 如何在折叠过程中获取成员类型的提升::mpl 占位符
- 将具有任意参数和占位符的函数存储在类中,然后再调用它
- 回调中的占位符_1是如何工作的
- 使用c++11占位符作为lambda