#include #include #include #include #include #include #include #include #include // struct to 'normalise' a function: takes a tuple of args // rather than the args directly template struct tuplised_function; // specialise tuplised_function for 1 argument template struct tuplised_function< Function, typename boost::enable_if_c< boost::function_type_arity::value == 1 >::type >{ typedef typename boost::function_type_parameters::type args_type; typedef typename boost::mpl::at_c::type args0_type; typedef boost::tuple< args0_type > tuple_type; typedef typename boost::function_type_result::type result_type; result_type operator ()(Function & f, tuple_type const & in) { args0_type first = in.get<0>(); return f(first); } }; // specialise tuplised_function for 2 argument template struct tuplised_function< Function, typename boost::enable_if_c< boost::function_type_arity::value ==2 >::type > { typedef typename boost::function_type_parameters::type args_type; typedef typename boost::mpl::at_c::type args0_type; typedef typename boost::mpl::at_c::type args1_type; typedef boost::tuple< args0_type,args1_type > tuple_type; typedef typename boost::function_type_result::type result_type; result_type operator ()(Function & f, tuple_type const & in) { args0_type first= in.get<0>(); args1_type second = in.get<1>(); return f(first,second); } }; // some simple function s to test bool is_less (double const & lhs , double const & rhs) { return lhs < rhs; } bool is_greater_than (double const & lhs , double const & rhs) { return lhs > rhs; } template< int N> double abs_diff(double const & in) { double t = in - N; return t >= 0 ? t : -t; } int main() { // create the wrapper struct to call the converted function typedef bool fun0(double const & , double const &); tuplised_function tf0; typedef double fun1(double const &); tuplised_function tf1; BOOST_AUTO(args1,(boost::make_tuple(20.))); BOOST_AUTO(args0,(boost::make_tuple(1.,2.))); std::cout << std::boolalpha << tf0(is_less,args0) <<'\n'; std::cout << std::boolalpha << tf0(is_greater_than,args0) <<'\n'; std::cout << tf1( abs_diff<220>,args1) <<'\n'; }