fork(1) download
  1. #include <stdio.h>
  2.  
  3. namespace named
  4. {
  5. template<typename RandomIt>
  6. void sort_it_it(RandomIt first, RandomIt last)
  7. {
  8. (void) last;
  9. (void) (first + 1);
  10. (void) (bool(*first < *first));
  11. // stuff...
  12. }
  13.  
  14. template<typename Container, typename Compare>
  15. void sort_cont_cmp(Container& c, Compare cmp)
  16. {
  17. typename Container::iterator b = c.begin(), e = c.end();
  18. (void) e;
  19. (void) (b + 1);
  20. (void) (bool(cmp(*b, *b)));
  21. // stuff...
  22. }
  23. }
  24.  
  25. namespace overloaded
  26. {
  27. template<typename RandomIt>
  28. void sort(RandomIt first, RandomIt last)
  29. {
  30. puts("sort(RandomIt, RandomIt)");
  31. named::sort_it_it(first, last);
  32. }
  33.  
  34. template<typename Container, typename Compare>
  35. void sort(Container& c, Compare cmp)
  36. {
  37. puts("sort(Container&, Compare)");
  38. named::sort_cont_cmp(c, cmp);
  39. }
  40. }
  41.  
  42. struct BadContCmp { // Not complete. Bad.
  43. int x[10];
  44.  
  45. BadContCmp() { for (int i = 0; i < 10; ++i) x[i] = 42; } // LOL.
  46.  
  47. typedef int * iterator;
  48. iterator begin() { return x; }
  49. iterator end() { return x + 10; }
  50.  
  51. typedef bool result_type;
  52. typedef int first_argument_type;
  53. typedef int second_argument_type;
  54. bool operator()(int const& a, int const& b) const { return a < b; }
  55. };
  56.  
  57. int main()
  58. {
  59. BadContCmp contcmp;
  60.  
  61. named::sort_it_it(contcmp.begin(), contcmp.end()); // ok (of course)
  62. named::sort_cont_cmp(contcmp, contcmp); // ok (of course)
  63.  
  64. overloaded::sort(contcmp.begin(), contcmp.end()); // ok
  65. overloaded::sort(contcmp, contcmp); // error (inside the body of sort_it_it)
  66. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘void named::sort_it_it(RandomIt, RandomIt) [with RandomIt = BadContCmp]’:
prog.cpp:31:32:   required from ‘void overloaded::sort(RandomIt, RandomIt) [with RandomIt = BadContCmp]’
prog.cpp:65:35:   required from here
prog.cpp:9:17: error: no match for ‘operator+’ (operand types are ‘BadContCmp’ and ‘int’)
   (void) (first + 1);
                 ^
prog.cpp:10:25: error: no match for ‘operator*’ (operand type is ‘BadContCmp’)
   (void) (bool(*first < *first));
                         ^
prog.cpp:10:16: error: no match for ‘operator*’ (operand type is ‘BadContCmp’)
   (void) (bool(*first < *first));
                ^
stdout

Standard output is empty