126 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s 2// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s3struct X { 4 X();5 X(int); 6};7 8X operator+(X, X);9X operator-(X, X) { X x; return x; }10 11struct 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 20void f(X x) {21 x = operator+(x, x);22}23 24X operator+(int, float); // expected-error{{overloaded 'operator+' must have at least one parameter of class or enumeration type}}25 26X operator*(X, X = 5); // expected-error{{parameter of overloaded 'operator*' cannot have a default argument}}27 28X operator/(X, X, ...); // expected-error{{overloaded 'operator/' cannot be variadic}}29 30X operator%(Y); // expected-error{{overloaded 'operator%' must be a binary operator (has 1 parameter)}}31 32void operator()(Y&, int, int); // expected-error{{overloaded 'operator()' must be a non-static member function}}33 34typedef int INT;35typedef float FLOAT;36Y& operator++(Y&);37Y operator++(Y&, INT);38X operator++(X&, FLOAT); // expected-error{{parameter of overloaded post-increment operator must have type 'int' (not 'FLOAT' (aka 'float'))}}39 40int operator+; // expected-error{{'operator+' cannot be the name of a variable or data member}}41 42namespace PR6238 {43 static struct {44 void operator()();45 } plus;46}47 48struct 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 53namespace PR14120 {54 struct A {55 static void operator()(int& i) { ++i; } // expected-warning{{is a C++23 extension}}56 };57 void f() {58 int i = 0;59 A()(i);60 }61}62 63namespace GH42535 {64class 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};69void operator+(E, ...) {} // expected-error{{overloaded 'operator+' cannot be variadic}}70void operator-(E, ...) {} // expected-error{{overloaded 'operator-' cannot be variadic}}71void operator*(E, ...) {} // expected-error{{overloaded 'operator*' cannot be variadic}}72void operator/(E, ...) {} // expected-error{{overloaded 'operator/' must be a binary operator}}73void operator/(E, E, ...) {} // expected-error{{overloaded 'operator/' cannot be variadic}}74void operator%(E, ...) {} // expected-error{{overloaded 'operator%' must be a binary operator}}75void operator%(E, E, ...) {} // expected-error{{overloaded 'operator%' cannot be variadic}}76E& operator++(E&, ...); // expected-error{{overloaded 'operator++' cannot be variadic}}77E& operator--(E&, ...); // expected-error{{overloaded 'operator--' cannot be variadic}}78bool operator<(const E& lhs, ...); // expected-error{{overloaded 'operator<' must be a binary operator}}79bool operator<(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}80bool operator>(const E& lhs, ...); // expected-error{{overloaded 'operator>' must be a binary operator}}81bool operator>(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}82bool operator>=(const E& lhs, ...); // expected-error{{overloaded 'operator>=' must be a binary operator}}83bool operator>=(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}84bool operator<=(const E& lhs, ...); // expected-error{{overloaded 'operator<=' must be a binary operator}}85bool operator<=(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}86bool operator==(const E& lhs, ...); // expected-error{{overloaded 'operator==' must be a binary operator}}87bool operator==(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}88bool operator!=(const E& lhs, ...); // expected-error{{overloaded 'operator!=' must be a binary operator}}89bool operator!=(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}90bool operator&&(const E& lhs, ...); // expected-error{{overloaded 'operator&&' must be a binary operator}}91bool operator&&(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}92bool operator||(const E& lhs, ...); // expected-error{{overloaded 'operator||' must be a binary operator}}93bool operator||(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}94bool operator>>(const E& lhs, ...); // expected-error{{overloaded 'operator>>' must be a binary operator}}95bool operator>>(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}}96bool operator&(const E& lhs, ...); // expected-error{{cannot be variadic}}97#if __cplusplus >= 202002L98auto operator<=>(const E& lhs, ...); // expected-error{{overloaded 'operator<=>' must be a binary operator}}99#endif100void 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 >= 202002L115 E() <=> E();116#endif117 E e;118 E e1 = e;119 e += e1;120 E() && E();121 E() || E();122 E() & E();123 E() >> E();124}125}126