fork(1) download
  1. template <typename T>
  2. struct Class {
  3. Class(T const &t) { }
  4. };
  5.  
  6. template <typename T_Lhs, typename T_Rhs>
  7. struct ClassAnd {
  8. ClassAnd(T_Lhs const &lhs, T_Rhs const &rhs) { }
  9. };
  10.  
  11. template <typename T, typename T_Rhs>
  12. ClassAnd<Class<T>, T_Rhs> operator&(Class<T> const &lhs, T_Rhs const &rhs) {
  13. return ClassAnd<Class<T>, T_Rhs>(lhs, rhs);
  14. }
  15.  
  16. template <typename T0, typename T1, typename T_Rhs>
  17. ClassAnd<ClassAnd<T0, T1>, T_Rhs> operator&(ClassAnd<T0, T1> const &lhs, T_Rhs const &rhs) {
  18. return ClassAnd<ClassAnd<T0, T1>, T_Rhs>(lhs, rhs);
  19. }
  20.  
  21. // Addition of ClassNot and the appropriate operators causes an ambiguity in operator&.
  22. template <typename T>
  23. struct ClassNot {
  24. ClassNot(T const &t) : value(t) { }
  25. T value;
  26. };
  27.  
  28. template <typename T_Lhs, typename T_Rhs>
  29. struct ClassAndNot {
  30. ClassAndNot(T_Lhs const &lhs, T_Rhs const &rhs) { }
  31. };
  32.  
  33. template <typename T_Lhs, typename T_Rhs>
  34. ClassAndNot<T_Lhs, T_Rhs> operator&(T_Lhs const &lhs, ClassNot<T_Rhs> const &rhs) {
  35. return ClassAndNot<T_Lhs, T_Rhs>(lhs, rhs.value);
  36. }
  37.  
  38. template <typename T_Rhs>
  39. ClassNot<T_Rhs> operator!(T_Rhs const &rhs) {
  40. return ClassNot<T_Rhs>(rhs);
  41. }
  42.  
  43. // Sample usage.
  44. int main() {
  45. Class<int> a(42);
  46. Class<double> b(3.14);
  47. auto c = a & !b;
  48. }
  49.  
Compilation error #stdin compilation error #stdout 0s 3336KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:47:13: error: ambiguous overload for ‘operator&’ (operand types are ‘Class<int>’ and ‘ClassNot<Class<double> >’)
  auto c = a & !b;
             ^
prog.cpp:47:13: note: candidates are:
prog.cpp:12:27: note: ClassAnd<Class<T>, T_Rhs> operator&(const Class<T>&, const T_Rhs&) [with T = int; T_Rhs = ClassNot<Class<double> >]
 ClassAnd<Class<T>, T_Rhs> operator&(Class<T> const &lhs, T_Rhs const &rhs) {
                           ^
prog.cpp:34:27: note: ClassAndNot<T_Lhs, T_Rhs> operator&(const T_Lhs&, const ClassNot<T_Rhs>&) [with T_Lhs = Class<int>; T_Rhs = Class<double>]
 ClassAndNot<T_Lhs, T_Rhs> operator&(T_Lhs const &lhs, ClassNot<T_Rhs> const &rhs) {
                           ^
stdout
Standard output is empty