make_overloaded_function 使ってみた。
前回の記事で『関数の型を書くのがめんどくさい』と書いたら id:RiSK さんから『make_overloaded_function があるよ』とコメントで教えて頂いたので試してみました。
[ソース]
#include <boost/functional/overloaded_function.hpp> #include <boost/lexical_cast.hpp> #include <string> #include <iostream> std::string to_string(int n){ return boost::lexical_cast<std::string>(n); } int to_int(std::string str){ return boost::lexical_cast<int>(str); } int main(){ auto multi = boost::make_overloaded_function(&to_string, &to_int); std::cout << multi(10) + "-mami" << std::endl; std::cout << multi("42") * 2 << std::endl; return 0; }
[出力]
10-mami 84
auto が使用できる環境であればこっちの方がよさそうですね。