fork(1) download
  1. // test.cpp
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct box {
  7. char maker[40];
  8. float height;
  9. float width;
  10. float length;
  11. float volume;
  12. };
  13.  
  14.  
  15. template <typename T>
  16. T max(T a, T b) {
  17. return a > b ? a : b;
  18. }
  19. template <> box max<box>(box a, box b) {
  20. return a.volume > b.volume ? a : b;
  21. }
  22.  
  23. int main() {
  24. box b = {"Cube", 2.3F, 1.5f, 4.1f, 32.0f};
  25. box c = {"Box", 2.6f, 2.3f, 1.2f, 26.8f};
  26. int i1 = 5, i2 = 3;
  27.  
  28. b = max(b, c);
  29. cout << max(i1, i2) << endl;
  30. cout << b.volume << endl;
  31.  
  32. return 0;
  33. }
  34.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:28:17: error: call of overloaded ‘max(box&, box&)’ is ambiguous
     b = max(b, c);
                 ^
prog.cpp:19:17: note: candidate: ‘T max(T, T) [with T = box]’
 template <> box max<box>(box a, box b) {
                 ^~~~~~~~
In file included from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:2:
/usr/include/c++/8/bits/stl_algobase.h:219:5: note: candidate: ‘constexpr const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = box]’
     max(const _Tp& __a, const _Tp& __b)
     ^~~
prog.cpp:29:23: error: call of overloaded ‘max(int&, int&)’ is ambiguous
     cout << max(i1, i2) << endl;
                       ^
prog.cpp:16:3: note: candidate: ‘T max(T, T) [with T = int]’
 T max(T a, T b) {
   ^~~
In file included from /usr/include/c++/8/bits/char_traits.h:39,
                 from /usr/include/c++/8/ios:40,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from prog.cpp:2:
/usr/include/c++/8/bits/stl_algobase.h:219:5: note: candidate: ‘constexpr const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]’
     max(const _Tp& __a, const _Tp& __b)
     ^~~
stdout
Standard output is empty