#include #include #include #include #include #include #include #include #include #include #include template< typename Tuple, size_t N = boost::tuples::length::value > struct function_feeder; template struct function_feeder{ template< class F > static typename boost::function_type_result< F >::type feed(F f, Tuple& ) { return f(); } }; template struct function_feeder{ template< class F > static typename boost::function_type_result< F >::type feed(F f, Tuple& t) { return f(boost::get<0>(t)); } }; template struct function_feeder{ template< class F > static typename boost::function_type_result< F >::type feed(F f, Tuple& t) { return f(boost::get<0>(t),boost::get<1>(t)); } }; template< class Function, class Tuple > typename boost::enable_if< boost::mpl::and_< boost::is_function_type< boost::function_pointer ,Function > //////////////////////// // replace equal_to with greater_equal for default args. //,boost::mpl::greater_equal< ,boost::mpl::equal_to< ////////////////////// boost::mpl::int_::value> ,boost::mpl::int_::value> > > ,typename boost::function_type_result< Function >::type >::type feed_function(Function f, Tuple& t) { return function_feeder::feed(f,t); } // 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; } void hello_f() { std::cout << "hello\n"; } int def_func(double a, int b = 0) { std::cout << a - b; return b; } int main() { BOOST_AUTO(args0,(boost::make_tuple(1,2))); int const val = boost::tuples::length >::value; std::cout << "LT "<< val <<'\n'; BOOST_AUTO(args1,(boost::make_tuple(20.))); std::cout << feed_function(is_less,args0)<< '\n'; std::cout << feed_function(is_less,args0)<< '\n'; std::cout << feed_function(abs_diff<200>,args1)<< '\n'; feed_function(hello_f, boost::make_tuple()); // ok without default args std::cout << feed_function(def_func, boost::make_tuple(1.,1)); // fails trying to use default args std::cout << feed_function(def_func, boost::make_tuple(1.)); }