fork download
  1. #include <iostream>
  2.  
  3. struct S
  4. {
  5. // template version
  6. template<class T>
  7. void method(T&& arg)
  8. {
  9. std::cout << __PRETTY_FUNCTION__ << '\n';
  10. }
  11.  
  12. // overload
  13. void method(std::string& arg)
  14. {
  15. std::cout << __PRETTY_FUNCTION__ << '\n';
  16. }
  17. };
  18.  
  19. int main()
  20. {
  21. S s;
  22.  
  23. // will invoke overload : perfect match
  24. std::string str = "Something";
  25. s.method(str);
  26.  
  27. // but not here
  28. s.method(10);
  29. s.method(20U);
  30. s.method("ref to const array");
  31.  
  32. // not here either (not lvalue reference).
  33. s.method(std::string("Not here"));
  34. }
  35.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
void S::method(std::string&)
void S::method(T&&) [with T = int]
void S::method(T&&) [with T = unsigned int]
void S::method(T&&) [with T = const char (&)[19]]
void S::method(T&&) [with T = std::basic_string<char>]