xref: /llvm-project/clang/test/SemaCXX/overloaded-operator-decl.cpp (revision af91092c449d6d4bcf60b0cd233c48cd8a41e0e1)
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
3 struct X {
4   X();
5   X(int);
6 };
7 
8 X operator+(X, X);
operator -(X,X)9 X operator-(X, X) { X x; return x; }
10 
11 struct Y {
12   Y operator-() const;
13   void operator()(int x = 17) const;
14   int operator[](int);
15 
16   static int operator+(Y, Y); // expected-error{{overloaded 'operator+' cannot be a static member function}}
17 };
18 
19 
f(X x)20 void f(X x) {
21   x = operator+(x, x);
22 }
23 
24 X operator+(int, float); // expected-error{{overloaded 'operator+' must have at least one parameter of class or enumeration type}}
25 
26 X operator*(X, X = 5); // expected-error{{parameter of overloaded 'operator*' cannot have a default argument}}
27 
28 X operator/(X, X, ...); // expected-error{{overloaded 'operator/' cannot be variadic}}
29 
30 X operator%(Y); // expected-error{{overloaded 'operator%' must be a binary operator (has 1 parameter)}}
31 
32 void operator()(Y&, int, int); // expected-error{{overloaded 'operator()' must be a non-static member function}}
33 
34 typedef int INT;
35 typedef float FLOAT;
36 Y& operator++(Y&);
37 Y operator++(Y&, INT);
38 X operator++(X&, FLOAT); // expected-error{{parameter of overloaded post-increment operator must have type 'int' (not 'FLOAT' (aka 'float'))}}
39 
40 int operator+; // expected-error{{'operator+' cannot be the name of a variable or data member}}
41 
42 namespace PR6238 {
43   static struct {
44     void operator()();
45   } plus;
46 }
47 
48 struct PR10839 {
49   operator int; // expected-error{{'operator int' cannot be the name of a variable or data member}}
50   int operator+; // expected-error{{'operator+' cannot be the name of a variable or data member}}
51 };
52 
53 namespace PR14120 {
54   struct A {
operator ()PR14120::A55     static void operator()(int& i) { ++i; } // expected-warning{{is a C++23 extension}}
56   };
f()57   void f() {
58     int i = 0;
59     A()(i);
60   }
61 }
62 
63 namespace GH42535 {
64 class E {
65   E& operator=(const E& rhs, ...); // expected-error{{overloaded 'operator=' cannot be variadic}}
66   E& operator+=(const E& rhs, ...); // expected-error{{overloaded 'operator+=' cannot be variadic}}
67 
68 };
operator +(E,...)69 void operator+(E, ...) {} // expected-error{{overloaded 'operator+' cannot be variadic}}
operator -(E,...)70 void operator-(E, ...) {} // expected-error{{overloaded 'operator-' cannot be variadic}}
operator *(E,...)71 void operator*(E, ...) {} // expected-error{{overloaded 'operator*' cannot be variadic}}
operator /(E,...)72 void operator/(E, ...) {} // expected-error{{overloaded 'operator/' must be a binary operator}}
operator /(E,E,...)73 void operator/(E, E, ...) {} // expected-error{{overloaded 'operator/' cannot be variadic}}
operator %(E,...)74 void operator%(E, ...) {} // expected-error{{overloaded 'operator%' must be a binary operator}}
operator %(E,E,...)75 void operator%(E, E, ...) {} // expected-error{{overloaded 'operator%' cannot be variadic}}
76 E& operator++(E&, ...); // expected-error{{overloaded 'operator++' cannot be variadic}}
77 E& operator--(E&, ...); // expected-error{{overloaded 'operator--' cannot be variadic}}
78 bool operator<(const E& lhs, ...); // expected-error{{overloaded 'operator<' must be a binary operator}}
79 bool operator<(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}
80 bool operator>(const E& lhs, ...); // expected-error{{overloaded 'operator>' must be a binary operator}}
81 bool operator>(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}
82 bool operator>=(const E& lhs, ...); // expected-error{{overloaded 'operator>=' must be a binary operator}}
83 bool operator>=(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}
84 bool operator<=(const E& lhs, ...); // expected-error{{overloaded 'operator<=' must be a binary operator}}
85 bool operator<=(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}
86 bool operator==(const E& lhs, ...); // expected-error{{overloaded 'operator==' must be a binary operator}}
87 bool operator==(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}
88 bool operator!=(const E& lhs, ...); // expected-error{{overloaded 'operator!=' must be a binary operator}}
89 bool operator!=(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}
90 bool operator&&(const E& lhs, ...); // expected-error{{overloaded 'operator&&' must be a binary operator}}
91 bool operator&&(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}
92 bool operator||(const E& lhs, ...); // expected-error{{overloaded 'operator||' must be a binary operator}}
93 bool operator||(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}
94 bool operator>>(const E& lhs, ...); // expected-error{{overloaded 'operator>>' must be a binary operator}}
95 bool operator>>(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}
96 bool operator&(const E& lhs, ...); // expected-error{{cannot be variadic}}
97 #if __cplusplus >= 202002L
98 auto operator<=>(const E& lhs, ...);  // expected-error{{overloaded 'operator<=>' must be a binary operator}}
99 #endif
d()100 void d() {
101   E() + E();
102   E() - E();
103   E() * E();
104   E() / E();
105   E() % E();
106   ++E(); // expected-error{{cannot increment value of type 'E'}}
107   --E(); // expected-error{{cannot decrement value of type 'E'}}
108   E() < E();
109   E() > E();
110   E() <= E();
111   E() >= E();
112   E() == E();
113   E() != E();
114 #if __cplusplus >= 202002L
115   E() <=> E();
116 #endif
117   E e;
118   E e1 = e;
119   e += e1;
120   E() && E();
121   E() || E();
122   E() & E();
123   E() >> E();
124 }
125 }
126