boost::function_types::result_type

boost::function_types::result_type は、関数型から戻り値型が取得できる template クラスである。
こんな感じ。

boost::function_types::result_type<int(*)()>::type;	// int
boost::function_types::result_type<void(*)(int)>::type;	// void


boost::result_of の処理と似ているが、template 引数へ渡す型が若干異なる。

typedef void(*func_t)(int);
boost::result_of<func_t(int)>::type;               // こっちは引数型が必要
boost::function_types::result_type<func_t>::type;  // こっちはいらない

boost::result_of は、引数に依存する関数(template 関数など)で使用し、boost::function_types::result_type は、関数の型が分かっているときに使用する。


例えば、この template クラスがどんな時に必要になるのかというと

template<
	typename func_t
>
???              // 何の型を返せばいいのか分からない\(^o^)/
call(func_t& func){
	return func();
}

int hogehoge(){
	return 10;
}
call(hogehoge);  // hogehoge() が呼ばれて欲しい

このように戻り値型が関数型に依存する場合などに使用する。



上記のような場合は、func_t の引数型が分からないので boost::result_of は使用できない。

boost::result_of<func_t(???)>::type   // 引数型が分かんなよ!


boost::function_types::result_type を使用すれば解決できる。

template<
	typename func_t
>
typename boost::function_types::result_type<func_t>::type // func_t の戻り値を返すよ
call(func_t& func){
	return func();
}

int hogehoge(){
	return 10;
}
assert( call(hogehoge) == 10);


こ様に使用する機会は割と限られるが、ピンポイントで必要になる場面があるので割と重宝する。
boost::function 関連の処理を実装しようとすると戻り値型や引数型が必要になってくるのでこのような template クラスが必須になってくる。
自力で result_type を実装しようとしたのは秘密だ!


あ、ちなみに関数オブジェクトは対応していないみたい。

struct object{
	void operator ()(){}
};
boost::function_types::result_type<object>::type; // type が未定義

まぁどこかを探せばありそうな気がする。


[boost]
ver 1.44.0
[参照]
http://www.kmonos.net/alang/boost/classes/result_of.html
http://d.hatena.ne.jp/faith_and_brave/20091006/