fork download
  1. #include <cstring>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. template <typename T>
  6. inline T const& max (T const& a, T const& b)
  7. {
  8. return a < b ? b : a;
  9. }
  10.  
  11. // maximum of two C-strings (call-by-value)
  12. inline char const* max (char const* a, char const* b)
  13. {
  14. return strcmp(a,b) < 0 ? b : a;
  15. }
  16.  
  17. // maximum of three values of any type (call-by-reference)
  18. template <typename T>
  19. inline T const& max (T const& a, T const& b, T const& c)
  20. {
  21. return max (max(a,b), c);
  22. }
  23.  
  24. int main(int argc, char* argv[])
  25. {
  26. std::cout << ::max(7, 42, 68) << std::endl;
  27. return 0;
  28. }
  29.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘const T& max(const T&, const T&, const T&) [with T = int]’:
prog.cpp:26:33:   required from here
prog.cpp:21:24: error: call of overloaded ‘max(const int&, const int&)’ is ambiguous
     return max (max(a,b), c); 
                        ^
prog.cpp:21:24: note: candidates are:
prog.cpp:6:17: note: const T& max(const T&, const T&) [with T = int]
 inline T const& max (T const& a, T const& b)
                 ^
prog.cpp:12:20: note: const char* max(const char*, const char*) <near match>
 inline char const* max (char const* a, char const* b)
                    ^
prog.cpp:12:20: note:   no known conversion for argument 2 from ‘const int’ to ‘const char*’
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from prog.cpp:2:
/usr/include/c++/4.8/bits/stl_algobase.h:216:5: note: const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]
     max(const _Tp& __a, const _Tp& __b)
     ^
prog.cpp: In function ‘const T& max(const T&, const T&, const T&) [with T = int]’:
prog.cpp:22:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty