boost::bind
便利。メンバデータも bind 出来るのが良い。
ちょっと気になる:
simple な functor を独自に書くのに比べると inline 化が抑止されるかもしれない。
boost::bind に置き換えるとコードサイズが微妙に小さくなるケースがあったので。
お題となるクラス
class Something { public: bool IsAdapt(int v) const { /* inline 化される単純なコード */ } };
ファンクタを書いた場合
struct Fn : public std::unary_function<const Something&, bool> , private Nonassignable { Fn(int v) : value_(v) { } Fn(const Fn& org) : value_(org.value_) { } bool operator()(const Something& st) { /* このコードも inline 化されるだろう */ return st.IsAdapt(value_); } private: value_; Fn(); /* Do not use */ }; ... どこかで it = std::find_if(something_.begin(), something_.end(), Fn(value));
boost::bind を使う
it = std::find_if(something_.begin(), something_.end(), boost:bind(&Something::IsAdapt, _1) == value); /* メンバ関数のアドレスを取り出す事で inline 化されないのでは? */
性能重視するなら、メンバ関数の bind はコードサイズが大きいもの限定の方が良いかも。