C ++ 11の新機能を試していると、std :: placeholders::_1をラムダとして直接使用できないことがわかりました。
#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のバージョンを使用するように変更すると、正常にコンパイルされます。
なぜこれが標準バージョンで機能しないのですか?それを機能させる方法はありますか、それともここで醜いラムダを使用する必要がありますか?
transform(a, a+5, a, [](int i){return i*2;});