fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <boost/bind.hpp>
  4. #include <boost/typeof/typeof.hpp>
  5.  
  6. struct overloaded
  7. {
  8. typedef int result_type;
  9.  
  10. int operator()(const std::string& s) const { return 1; }
  11. int operator()(double d) const { return 2; }
  12. };
  13.  
  14. struct factory
  15. {
  16. template <typename T> struct result { typedef BOOST_TYPEOF_TPL(boost::bind(overloaded(), T())) type; };
  17.  
  18. template <typename T>
  19. typename result<T>::type operator()(T t) const
  20. {
  21. return boost::bind(overloaded(), t);
  22. }
  23. };
  24.  
  25. int main()
  26. {
  27. overloaded foo;
  28.  
  29. // based on local bind expression:
  30. BOOST_AUTO(unresolved, boost::bind(foo, _1));
  31. std::cout << unresolved("3.14") << std::endl; // should print 1
  32. std::cout << unresolved(3.14) << std::endl; // should print 2
  33.  
  34. // based on a factory function template
  35. factory makefoo;
  36. std::cout << makefoo("3.14")() << std::endl; // should print 1
  37. std::cout << makefoo(3.14)() << std::endl; // should print 2
  38. }
Success #stdin #stdout 0s 2856KB
stdin
Standard input is empty
stdout
1
2
1
2