fork download
  1. #include "stdio.h"
  2.  
  3. namespace A {
  4. template <typename T>
  5. struct Key {
  6. T value;
  7. };
  8.  
  9. template <
  10. typename T,
  11. template<typename> class KeyType
  12. >
  13. bool operator==(const KeyType<T> &a, const KeyType<T> &b) {
  14. return a.value == b.value;
  15. }
  16. }
  17. namespace B {
  18. template <typename KT>
  19. struct MappedVal {
  20. KT key;
  21. };
  22.  
  23. template <
  24. typename KT,
  25. template<typename> class MappedType
  26. >
  27. bool operator==(const MappedType<KT> &a, const MappedType<KT> &b) {
  28. return (a.key == b.key);
  29. }
  30. }
  31.  
  32. int main() {
  33. A::Key<short> key1, key2;
  34. key1.value = 1;
  35. key2.value = 1;
  36.  
  37. B::MappedVal<A::Key<short> > val1, val2;
  38. val1.key = key1;
  39.  
  40. val2.key = key1;
  41.  
  42. bool test1 = (key1 == key2);
  43. bool test2 = (val1 == val2);
  44.  
  45. printf("key1 == key2: %s\n", (test1 ? "true" : "false"));
  46. printf("val1 == val2: %s\n", (test2 ? "true" : "false"));
  47.  
  48. return 0;
  49. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:43:22: error: ambiguous overload for ‘operator==’ (operand types are ‘B::MappedVal<A::Key<short int> >’ and ‘B::MappedVal<A::Key<short int> >’)
   bool test2 = (val1 == val2);
                      ^
prog.cpp:43:22: note: candidates are:
prog.cpp:27:10: note: bool B::operator==(const MappedType<KT>&, const MappedType<KT>&) [with KT = A::Key<short int>; MappedType = B::MappedVal]
     bool operator==(const MappedType<KT> &a, const MappedType<KT> &b) {
          ^
prog.cpp:13:10: note: bool A::operator==(const KeyType<T>&, const KeyType<T>&) [with T = A::Key<short int>; KeyType = B::MappedVal]
     bool operator==(const KeyType<T> &a, const KeyType<T> &b) {
          ^
stdout
Standard output is empty