brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · 95d6a55 Raw
49 lines · cpp
1// RUN: %clang_cc1 -std=c++2a -verify %s2 3// ... return type shall be cv bool ...4namespace not_bool {5  struct X {} x;6  struct Y {} y;7  double operator==(X, Y); // expected-note 4{{here}}8  bool a = x == y; // ok9  bool b = y == x; // expected-error {{return type 'double' of selected 'operator==' function for rewritten '==' comparison is not 'bool'}}10  bool c = x != y; // expected-error {{return type 'double' of selected 'operator==' function for rewritten '!=' comparison is not 'bool'}}11  bool d = y != x; // expected-error {{return type 'double' of selected 'operator==' function for rewritten '!=' comparison is not 'bool'}}12 13  // cv-qualifiers are OK14  const bool operator==(Y, X);15  bool e = y != x; // ok16 17  // We don't prefer a function with bool return type over one witn non-bool return type.18  bool f = x != y; // expected-error {{return type 'double' of selected 'operator==' function for rewritten '!=' comparison is not 'bool'}}19 20  // As an extension, we permit integral and unscoped enumeration types too.21  // These are used by popular C++ libraries such as ICU.22  struct Z {} z;23  int operator==(X, Z); // expected-note {{here}}24  bool g = z == x; // expected-warning {{ISO C++20 requires return type of selected 'operator==' function for rewritten '==' comparison to be 'bool', not 'int'}}25 26  enum E {};27  E operator==(Y, Z); // expected-note {{here}}28  bool h = z == y; // expected-warning {{ISO C++20 requires return type of selected 'operator==' function for rewritten '==' comparison to be 'bool', not 'E'}}29}30 31struct X { bool equal; };32struct Y {};33constexpr bool operator==(X x, Y) { return x.equal; }34 35static_assert(X{true} == Y{});36static_assert(X{false} == Y{}); // expected-error {{failed}}37 38// x == y -> y == x39static_assert(Y{} == X{true});40static_assert(Y{} == X{false}); // expected-error {{failed}}41 42// x != y -> !(x == y)43static_assert(X{true} != Y{}); // expected-error {{failed}}44static_assert(X{false} != Y{});45 46// x != y -> !(y == x)47static_assert(Y{} != X{true}); // expected-error {{failed}}48static_assert(Y{} != X{false});49