stl_function.h

http://boost.cppll.jp/HEAD/libs/lambda/doc/ar01s03.html

template <class T> : public binary_function<T, T, T>
struct plus {
  T operator()(const T& i, const T& j) const {
    return i + j; 
  }
};

文法がわからない…。

/stl_function.h - Google ソースコード検索

template <class _Tp>
struct plus : public binary_function<_Tp,_Tp,_Tp> {
  _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
};

これはわかる。下に書いたbinary_functionって構造体を継承して、()演算子を定義して、呼ばれたときに足した結果が返るようにしている。

template <class _Arg1, class _Arg2, class _Result>
struct binary_function {
  typedef _Arg1 first_argument_type;
  typedef _Arg2 second_argument_type;
  typedef _Result result_type;
};

Pythonの場合、()演算子を定義する場合は__call__メソッドを定義することになる。たとえレシーバが必要ない場合でもインスタンスを作る必要がある。

>>> class Fanctor(object):
	def __call__(self, x, y):
		return x + y

	
>>> add = Fanctor()
>>> add(1, 2)
3

C++の方がファンクタの作り方としては自然かもしれない。