#include <boost/any.hpp>
#include <string>
#include <functional>
#include <iostream>
#include <exception>
template < class T, class OPE = std::binary_function<T,T,bool> >
bool in_type_ope( boost::any data, boost::any value, OPE ope ) {
try {
return ope( boost::any_cast<T>(data), boost::any_cast<T>(value) );
} catch( std::exception& e ) {
return false;
}
}
int main() {
boost::any a = 1;
boost::any b = 2;
std::cout << in_type_ope<int>( a, b, std::less<int>() ) << std::endl;
std::cout << in_type_ope<int>( b, a, std::less<int>() ) << std::endl;
a = 2;
std::cout << in_type_ope<int>( a, b, std::equal_to<int>() ) << std::endl;
return 0;
}
2013年5月1日水曜日
boost::any で比較
データベースのデータを透過的に扱おうとすると、型が〜型が〜って、結構面倒くさい。
なるべくなら、楽をしたいです。もっとスマートな書き方無いのかな。
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿