[function] support for overloaded function

Hello, how can I support the return type of bind as well as function as function argument in following code. regards, Oliver #include <iostream> #include <cstdlib> #include <stdexcept> #include <boost/bind.hpp> #include <boost/function.hpp> struct X {}; struct Y { template< typename R > void f( boost::function< R() > const& fn) {} template< typename R > void f( boost::function< R( X &) > const& fn) {} }; struct Z { int u( int i) { return i; } int v( X & x, int i) { return i; } }; int main( int argc, char *argv[]) { try { Y y; X x; Z z; // does not compile y.f( boost::bind( & Z::u, z, 1) ); boost::function< int() > fn1( boost::bind( & Z::u, z, 1) ); y.f( fn1); // does not compile y.f( boost::bind( & Z::v, z, _1, 1) ); boost::function< int( X &) > fn2( boost::bind( & Z::v, z, _1, 1) ); y.f( fn2); return EXIT_SUCCESS; } catch ( std::exception const& e) { std::cerr << e.what() << std::endl; } catch ( ... ) { std::cerr << "unhandled" << std::endl; } return EXIT_FAILURE; } -- Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

Oliver Kowalke:
Hello, how can I support the return type of bind as well as function as function argument in following code.
...
struct Y { template< typename R > void f( boost::function< R() > const& fn) {}
template< typename R > void f( boost::function< R( X &) > const& fn) {} };
You can't; a nullary boost::bind expression can be converted to either function<> and you can't tell with certainty which one the user has meant. For the return type you can do: struct Y { template< typename R > void f1_( boost::function< R() > const& fn) { ... } template<class F> void f1( F f ) { f1_( function<typename F::result_type>( f ) ); } }; but I see no reason to prefer the above over just doing: struct Y { template<class F> void f1( F f ) { ... } }; -- Peter Dimov http://www.pdplayer.com
participants (2)
-
Oliver Kowalke
-
Peter Dimov