boost::lambda で遊んでみましたが、解決方法がわからないエラーが発生しました。
私はこれが初心者のエラーであると感じているので、私の無知を許してください(そして、ブーストラムダのドキュメント全体も読まなかった私の怠惰を認めなければなりません)。
場合によっては、boost::bind (またはboost::lambda::bind?) を使用する方がboost::lambda よりも適しているようですが、ここで適用できるかどうかはわかりません。目的に反するため、用に別の関数を作成する必要はありませんif cond(arg1) arg2.insert(arg1) ;
。私が推測するファンクターよりもはるかに優れているとは言えません。
仕事でVC9でブースト1.35を使用しています。エラーは、cond()
およびinsert()
呼び出し元のサイトにあります:「C2664: 'boost::lambda::placeholder1_type からパラメーター 1 を変換できません」
Cygwin で g++ を使用して、このスニペットの問題を再現しました。
#include <boost/function.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/if.hpp>
#include <boost/foreach.hpp>
#include <iostream>
#include <set>
void work( boost::function<void(long)> doAction ) {
long results[] = { 2, 5, 4 };
BOOST_FOREACH( long r, results )
doAction( r );
}
bool cond( long r ) { return r % 2 == 0 ; }
int main() {
using namespace boost::lambda;
std::set<long> myResults;
work(
if_then( cond(_1) , boost::ref(myResults).get().insert(_1) ) );
BOOST_FOREACH( long r, myResults )
std::cout << r << "\n";
}
g++ エラー:
lambda_test.cpp: In function ‘int main()’:
lambda_test.cpp:21:19: error: cannot convert ‘boost::lambda::placeholder1_type {aka const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >}’ to ‘long int’ for argument ‘1’ to ‘bool cond(long int)’
if_then( cond(_1) , boost::ref(myResults).get().insert(_1) ) );
^
lambda_test.cpp:21:60: error: no matching function for call to ‘std::set<long int>::insert(boost::lambda::placeholder1_type&)’
if_then( cond(_1) , boost::ref(myResults).get().insert(_1) ) );
どんな助けでも大歓迎です、
ありがとう