Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 1 | // RUN: clang -fsyntax-only -verify %s |
| 2 | class X { }; |
| 3 | |
| 4 | X operator+(X, X); |
| 5 | |
| 6 | void f(X x) { |
| 7 | x = x + x; |
| 8 | } |
| 9 | |
| 10 | struct Y; |
| 11 | struct Z; |
| 12 | |
| 13 | struct Y { |
| 14 | Y(const Z&); |
| 15 | }; |
| 16 | |
| 17 | struct Z { |
| 18 | Z(const Y&); |
| 19 | }; |
| 20 | |
| 21 | Y operator+(Y, Y); |
| 22 | bool operator-(Y, Y); // expected-note{{candidate function}} |
| 23 | bool operator-(Z, Z); // expected-note{{candidate function}} |
| 24 | |
| 25 | void g(Y y, Z z) { |
| 26 | y = y + z; |
| 27 | bool b = y - z; // expected-error{{use of overloaded operator '-' is ambiguous; candidates are:}} |
| 28 | } |
| 29 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 30 | struct A { |
| 31 | bool operator==(Z&); // expected-note{{candidate function}} |
| 32 | }; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 34 | A make_A(); |
| 35 | |
| 36 | bool operator==(A&, Z&); // expected-note{{candidate function}} |
| 37 | |
| 38 | void h(A a, const A ac, Z z) { |
| 39 | make_A() == z; |
| 40 | a == z; // expected-error{{use of overloaded operator '==' is ambiguous; candidates are:}} |
| 41 | ac == z; // expected-error{{invalid operands to binary expression ('struct A const' and 'struct Z')}} |
| 42 | } |
| 43 | |
| 44 | struct B { |
| 45 | bool operator==(const B&) const; |
| 46 | |
| 47 | void test(Z z) { |
| 48 | make_A() == z; |
| 49 | } |
| 50 | }; |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 51 | |
| 52 | enum Enum1 { }; |
| 53 | enum Enum2 { }; |
| 54 | |
| 55 | struct E1 { |
| 56 | E1(Enum1) { } |
| 57 | }; |
| 58 | |
| 59 | struct E2 { |
| 60 | E2(Enum2); |
| 61 | }; |
| 62 | |
| 63 | // C++ [over.match.oper]p3 - enum restriction. |
| 64 | float& operator==(E1, E2); |
| 65 | |
| 66 | void enum_test(Enum1 enum1, Enum2 enum2, E1 e1, E2 e2) { |
| 67 | float &f1 = (e1 == e2); |
| 68 | float &f2 = (enum1 == e2); |
| 69 | float &f3 = (e1 == enum2); |
| 70 | float &f4 = (enum1 == enum2); // expected-error{{non-const reference to type 'float' cannot be initialized with a temporary of type '_Bool'}} |
| 71 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | struct PostInc { |
| 75 | PostInc operator++(int); |
| 76 | PostInc& operator++(); |
| 77 | }; |
| 78 | |
| 79 | struct PostDec { |
| 80 | PostDec operator--(int); |
| 81 | PostDec& operator--(); |
| 82 | }; |
| 83 | |
| 84 | void incdec_test(PostInc pi, PostDec pd) { |
| 85 | const PostInc& pi1 = pi++; |
| 86 | const PostDec& pd1 = pd--; |
| 87 | PostInc &pi2 = ++pi; |
| 88 | PostDec &pd2 = --pd; |
| 89 | } |
| 90 | |
| 91 | struct SmartPtr { |
| 92 | int& operator*(); |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame^] | 93 | long& operator*() const volatile; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame^] | 96 | void test_smartptr(SmartPtr ptr, const SmartPtr cptr, |
| 97 | const volatile SmartPtr cvptr) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 98 | int &ir = *ptr; |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame^] | 99 | long &lr = *cptr; |
| 100 | long &lr2 = *cvptr; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 101 | } |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | struct ArrayLike { |
| 105 | int& operator[](int); |
| 106 | }; |
| 107 | |
| 108 | void test_arraylike(ArrayLike a) { |
| 109 | int& ir = a[17]; |
| 110 | } |
| 111 | |
| 112 | struct SmartRef { |
| 113 | int* operator&(); |
| 114 | }; |
| 115 | |
| 116 | void test_smartref(SmartRef r) { |
| 117 | int* ip = &r; |
| 118 | } |
| 119 | |
| 120 | bool& operator,(X, Y); |
| 121 | |
| 122 | void test_comma(X x, Y y) { |
| 123 | bool& b1 = (x, y); |
| 124 | X& xr = (x, x); |
| 125 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 126 | |
| 127 | |
| 128 | struct Callable { |
| 129 | int& operator()(int, double = 2.71828); // expected-note{{candidate function}} |
| 130 | float& operator()(int, double, long, ...); // expected-note{{candidate function}} |
| 131 | }; |
| 132 | |
| 133 | void test_callable(Callable c) { |
| 134 | int &ir = c(1); |
| 135 | float &fr = c(1, 3.14159, 17, 42); |
| 136 | |
| 137 | c(); // expected-error{{no matching function for call to object of type 'struct Callable'; candidates are:}} |
| 138 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 139 | |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 140 | typedef float FLOAT; |
| 141 | typedef int& INTREF; |
| 142 | typedef INTREF Func1(FLOAT, double); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 143 | typedef float& Func2(int, double); |
| 144 | |
| 145 | struct ConvertToFunc { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 146 | operator Func1*(); // expected-note{{conversion candidate of type 'INTREF (*)(FLOAT, double)'}} |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 147 | operator Func2&(); // expected-note{{conversion candidate of type 'float &(&)(int, double)'}} |
Douglas Gregor | 9ebae31 | 2008-11-19 22:59:19 +0000 | [diff] [blame] | 148 | void operator()(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | void test_funcptr_call(ConvertToFunc ctf) { |
| 152 | int &i1 = ctf(1.0f, 2.0); |
| 153 | float &f2 = ctf((short int)1, 1.0f); |
| 154 | ctf((long int)17, 2.0); // expected-error{{error: call to object of type 'struct ConvertToFunc' is ambiguous; candidates are:}} |
Douglas Gregor | 9ebae31 | 2008-11-19 22:59:19 +0000 | [diff] [blame] | 155 | ctf(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 156 | } |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 157 | |
| 158 | struct HasMember { |
| 159 | int m; |
| 160 | }; |
| 161 | |
| 162 | struct Arrow1 { |
| 163 | HasMember* operator->(); |
| 164 | }; |
| 165 | |
| 166 | struct Arrow2 { |
| 167 | Arrow1 operator->(); // expected-note{{candidate function}} |
| 168 | }; |
| 169 | |
| 170 | void test_arrow(Arrow1 a1, Arrow2 a2, const Arrow2 a3) { |
| 171 | int &i1 = a1->m; |
| 172 | int &i2 = a2->m; |
| 173 | a3->m; // expected-error{{no viable overloaded 'operator->'; candidates are}} |
| 174 | } |