fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <functional>
  4.  
  5. using namespace std::placeholders;
  6.  
  7. struct overloaded
  8. {
  9. int operator()(const std::string& s) const { return 1; }
  10. int operator()(double d) const { return 2; }
  11. };
  12.  
  13. template <typename T>
  14. auto makefoo(const T& t) -> decltype(std::bind(overloaded(), t))
  15. {
  16. return std::bind(overloaded(), t);
  17. }
  18.  
  19. int main()
  20. {
  21. overloaded foo;
  22.  
  23. // based on local bind expression:
  24. auto unresolved = std::bind(foo, _1);
  25. std::cout << unresolved(3.14) << std::endl; // should print 2
  26. std::cout << unresolved("3.14") << std::endl; // should print 1
  27.  
  28. // based on a factory function template
  29. std::cout << makefoo(3.14)() << std::endl; // should print 2
  30. std::cout << makefoo("3.14")() << std::endl; // should print 1
  31. }
Success #stdin #stdout 0s 2960KB
stdin
Standard input is empty
stdout
2
1
2
1