私は私のクラスはFooのスマートPTRのベクトルを持っている:何の代替はstd ::へのstdを使用しているときNOT1を使用する:: TR1 ::バインドまたはのstd :: tr1を:: mem_fnの
struct Foo
{
Foo() : mEnabled(false) {}
bool mEnabled;
bool isEnabled() const { return mEnabled; }
void setEnabled(bool inEnabled) { mEnabled = inEnabled; }
/* ... */
};
typedef std::tr1::shared_ptr<Foo> tFooPtr;
typedef std::vector<tFooPtr> tFooVec;
私は、このうまく働いています:
tFooVec foo_vector; // insert couple of elements
size_t count = count_if(foo_vector.begin(), foo_vector.end(), std::tr1::mem_fn(&Foo::isEnabled));
しかし、機能的なものを「ヘルパー」を使用するために、私は「無効」fooがC
size_t count = count_if(foo_vector.begin(), foo_vector.end(), std::not1(std::tr1::mem_fn(&Foo::isEnabled))); // does not compile
線の上にいないオブジェクトをcount_ifしたいときにompile:
(Linux上でG ++ 4.1.2を使用して)/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:446: error: no match for call to '(std::unary_negate<std::tr1::_Mem_fn<bool (Foo::*)()const> >) (std::tr1::shared_ptr<Foo>&)'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:322: note: candidates are: bool std::unary_negate<_Predicate>::operator()(const typename _Predicate::argument_type&) const [with _Predicate = std::tr1::_Mem_fn<bool (Foo::*)()const>]
make: *** [src/shared_ptr_tests.o] Error 1
私は、コンパイルの問題はstd::not1
関数/述語がPredicate::argument_type
を提供することを必要とするstd::unary_negate
を使用しているという事実から来ていると思います。述語が私はstd::tr1::mem_fn
はstd::unary_function
を使用してもargument_type
を提供していないと仮定std::unary_function
ため息これを言った
に由来する場合、後者が与えられます。私は今、合併症(と混乱を)避けるために代わりのstdのバインド:: :: tr1を::バインド
#include <boost/bind.hpp>
using namespace boost;
...
size_t countboost = count_if(foo_vector.begin(), foo_vector.end(), !(bind(&Foo::isEnabled, _1)));
私は今のブーストを使用していますということであり、使用しています
ソリューションは、私はの使用をreplacde私のコード全体でboost :: bindとstd :: tr1 :: bind。 !
最新のコンパイラ(C++ 11)を使用できる場合は、代わりにラムダを使用できます。 – stefaanv
は私のコードがコンパイルされているシステムは自分で所有していないことができます。そしてそれを悪化させるために、私のコードはxlCでAIX上でコンパイルする必要があります。だから、私は "昔ながらの"ファンクタに固執しています。 –
不合理な答え: 'size_t count = v.size() - std :: count_if(v.begin()、v.end()、std :: tr1 :: mem_fn(&Foo :: isEnabled))'。時には最も簡単な答えは問題を回避することです。 –