マイナーな演算子をオーバーロードして

http://d.hatena.ne.jp/faith_and_brave/20080229/1204277111


思いつきなので細かいことは気にしない方向で。

struct Foo {
	int i;
	template<typename F>
	typename boost::enable_if_c<
		boost::function_traits<typename boost::remove_pointer<F>::type>::arity == 1,
		boost::function<typename boost::function_traits<typename boost::remove_pointer<F>::type>::result_type ()>
	>::type operator->*(F f) {
		return boost::bind(f, *this);
	}
	template<typename F>
	typename boost::enable_if_c<
		boost::function_traits<typename boost::remove_pointer<F>::type>::arity == 2,
		boost::function<typename boost::function_traits<typename boost::remove_pointer<F>::type>::result_type (typename boost::function_traits<typename boost::remove_pointer<F>::type>::arg2_type)>
	>::type operator->*(F f) {
		return boost::bind(f, *this, _1);
	}
	template<typename F>
	typename boost::enable_if_c<
		boost::function_traits<typename boost::remove_pointer<F>::type>::arity == 3,
		boost::function<typename boost::function_traits<typename boost::remove_pointer<F>::type>::result_type (typename boost::function_traits<typename boost::remove_pointer<F>::type>::arg2_type, typename boost::function_traits<typename boost::remove_pointer<F>::type>::arg3_type)>
	>::type operator->*(F f) {
		return boost::bind(f, *this, _1, _2);
	}

	Foo() : i(1) {}
};

void f(Foo& foo) {std::cout << 42 << std::endl;}
void g(Foo& foo, int i) {std::cout << foo.i << ", " << i << std::endl;}
double h(Foo& foo, int i, double d) {return i + d;}

// テスト
Foo foo;
(foo->*f)();  // 42
(foo->*g)(2);  // 1, 2
std::cout << (foo->*h)(1, 2.5f) << std::endl;  // 3.5


もっとまともな方法があるかも知れないと思いつつも私には思いつかなった。それ以前にこれを使おうとも思わないが。


えんいー