| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do compile { target c++20 } } |
| 19 | |
| 20 | #include <compare> |
| 21 | |
| 22 | template<typename C, typename T, typename U> |
| 23 | concept comparable = requires (const C& cmp, const T& t, const U& u) { |
| 24 | cmp(t, u); |
| 25 | }; |
| 26 | |
| 27 | void |
| 28 | test01() |
| 29 | { |
| 30 | struct X |
| 31 | { |
| 32 | operator int() const { return 0; } |
| 33 | operator long*() const { return nullptr; } |
| 34 | } x; |
| 35 | |
| 36 | // Not three-way-comparable because of ambiguous conversion to int or long*: |
| 37 | static_assert( ! std::three_way_comparable<X> ); |
| 38 | |
| 39 | // And therefore X is not three-way-comparable-with anything else |
| 40 | // (because std::three_way_comparable_with<X, Y> requires that both |
| 41 | // three_way_comparable<X> and three_way_comparable<Y> are true). |
| 42 | static_assert( ! std::three_way_comparable_with<X, long*> ); |
| 43 | |
| 44 | long l; |
| 45 | // But <=> is valid and resolves to a builtin operator comparing pointers: |
| 46 | [[maybe_unused]] auto c = &l <=> x; |
| 47 | |
| 48 | // But LWG 3530 says std::compare_three_way should not be usable: |
| 49 | static_assert( ! comparable<std::compare_three_way, long*, X> ); |
| 50 | static_assert( ! comparable<std::compare_three_way, X, long*> ); |
| 51 | } |