// { dg-do run { target c++20 } } #include #include #include #include constexpr const char arr[] = "efgh\0abcd\0ijkl"; constexpr const char* s1 = arr; constexpr const char* s2 = arr+5; constexpr const char* s3 = arr+6; struct CStrNone { const char* str; constexpr operator const char*() const { return str; } }; template struct CStrMem { const char* str; constexpr operator const char*() const { return str; } auto operator<=>(CStrMem const& rhs) const { return ResultCreator::create(std::strcmp(this->str, rhs.str)); } auto operator<=>(const char* rhs) const { return ResultCreator::create(std::strcmp(this->str, rhs)); } }; template struct CStrFriend { const char* str; constexpr operator const char*() const { return str; } friend auto operator<=>(CStrFriend lhs, CStrFriend rhs) { return ResultCreator::create(std::strcmp(lhs.str, rhs.str)); } friend auto operator<=>(CStrFriend lhs, const char* rhs) { return ResultCreator::create(std::strcmp(lhs.str, rhs)); } }; template struct CStrFree { const char* str; constexpr operator const char*() const { return str; } }; template auto operator<=>(CStrFree lhs, CStrFree rhs) { return RC::create(std::strcmp(lhs.str, rhs.str)); } template auto operator<=>(CStrFree lhs, const char* rhs) { return RC::create(std::strcmp(lhs.str, rhs)); } template struct CStrMixed { const char* str; constexpr operator const char*() const { return str; } }; template auto operator<=>(CStrMixed lhs, CStrMixed rhs) { return RC::create(std::strcmp(lhs.str, rhs.str)); } template auto operator<=>(CStrMixed lhs, CStrFree rhs) { return RC::create(std::strcmp(lhs.str, rhs.str)); } // If the type returned from shapeship does not support relational // operators, then synthesized operators are ill-formed, SFINAEable. struct ReturnVoid { constexpr static void create(int) { } }; struct NoOperators { constexpr static NoOperators create(int) { return NoOperators(); } }; // std defined ordering types are expected template struct ReturnOrd { constexpr static Ord create(int cmp) { return cmp <=> 0; } }; // However, other types that provide required // operators are supported. struct ReturnInt { constexpr static int create(int cmp) { return cmp; } }; struct CustomOrd { constexpr static CustomOrd create(int cmp) { return CustomOrd(cmp); } CustomOrd() = default; explicit constexpr CustomOrd(int cmp) : v(cmp) {} friend constexpr bool operator<(CustomOrd c, std::nullptr_t) { return c.v < 0; } friend constexpr bool operator<(std::nullptr_t, CustomOrd c) { return 0 < c.v; } friend constexpr bool operator>(CustomOrd c, std::nullptr_t) { return c.v > 0; } friend constexpr bool operator>(std::nullptr_t, CustomOrd c) { return 0 > c.v; } friend constexpr bool operator<=(CustomOrd c, std::nullptr_t) { return c.v <= 0; } friend constexpr bool operator<=(std::nullptr_t, CustomOrd c) { return 0 <= c.v; } friend constexpr bool operator>=(CustomOrd c, std::nullptr_t) { return c.v >= 0; } friend constexpr bool operator>=(std::nullptr_t, CustomOrd c) { return 0 >= c.v; } friend constexpr CustomOrd operator<=>(CustomOrd c, std::nullptr_t) { return c; } friend constexpr CustomOrd operator<=>(std::nullptr_t, CustomOrd c) { return CustomOrd(-c.v); } private: int v = 0; }; template void test_relational(bool use_overloaded) { CStr1 cs1{s1}; CStr2 cs2{s2}; if (use_overloaded) { // Overloaded operaetors compare content of the string, // and cs1 > cs2; VERIFY( !(cs1 < cs2) ); VERIFY( !std::less<>{}(cs1, cs2) ); VERIFY( !std::ranges::less{}(cs1, cs2) ); VERIFY( (cs1 > cs2) ); VERIFY( std::greater<>{}(cs1, cs2) ); VERIFY( std::ranges::greater{}(cs1, cs2) ); VERIFY( !(cs1 < cs2) ); VERIFY( !std::less_equal<>{}(cs1, cs2) ); VERIFY( !std::ranges::less_equal{}(cs1, cs2) ); VERIFY( (cs1 > cs2) ); VERIFY( std::greater_equal<>{}(cs1, cs2) ); VERIFY( std::ranges::greater_equal{}(cs1, cs2) ); } else { // Without overloaded operators, we comapre pointers, // and cs1 < cs2; VERIFY( (cs1 < cs2) ); VERIFY( std::less<>{}(cs1, cs2) ); VERIFY( std::ranges::less{}(cs1, cs2) ); VERIFY( !(cs1 > cs2) ); VERIFY( !std::greater<>{}(cs1, cs2) ); VERIFY( !std::ranges::greater{}(cs1, cs2) ); VERIFY( (cs1 < cs2) ); VERIFY( std::less_equal<>{}(cs1, cs2) ); VERIFY( std::ranges::less_equal{}(cs1, cs2) ); VERIFY( !(cs1 > cs2) ); VERIFY( !std::greater_equal<>{}(cs1, cs2) ); VERIFY( !std::ranges::greater_equal{}(cs1, cs2) ); } } template void test_relational_type(bool use_overloaded) { test_relational(use_overloaded); test_relational(use_overloaded); test_relational(use_overloaded); } template void test_relational_return() { test_relational_type>(true); test_relational_type>(true); test_relational_type>(true); test_relational, CStrFree>(true); test_relational, CStrMixed>(true); } template void test_spaceship(bool use_overloaded) { CStr1 cs1{s1}; CStr2 cs2{s2}; if (use_overloaded) { // Overloaded operaetors compare content of the string, // and cs1 > cs2; VERIFY( (cs1 <=> cs2) > 0 ); VERIFY( std::compare_three_way{}(cs1, cs2) > 0 ); } else { // Without overloaded operators, we comapre pointers, // and cs1 < cs2; VERIFY( (cs1 <=> cs2) < 0 ); VERIFY( std::compare_three_way{}(cs1, cs2) < 0 ); } } template void test_spaceship_type(bool use_overloaded) { test_spaceship(use_overloaded); test_spaceship(use_overloaded); test_spaceship(use_overloaded); } template void test_std_ordering() { using RC = ReturnOrd; test_relational_return(); test_spaceship_type>(true); test_spaceship_type>(true); test_spaceship_type>(true); test_spaceship, CStrFree>(true); test_spaceship, CStrMixed>(true); } template void test_no_relational() { CStr1 c1{}; CStr2 c2{}; static_assert(!requires { c1 < c2; }); static_assert(!requires { c1 > c2; }); static_assert(!requires { c1 <= c2; }); static_assert(!requires { c1 >= c2; }); } template void test_no_relational_type() { test_no_relational(); test_no_relational(); test_no_relational(); } template void test_no_relational_return() { test_no_relational_type>(); test_no_relational_type>(); test_no_relational_type>(); test_no_relational, CStrFree>(); test_no_relational, CStrMixed>(); } int main() { test_std_ordering(); test_std_ordering(); test_std_ordering(); test_relational_type(false); test_relational_return(); test_relational_return(); test_no_relational_return(); test_no_relational_return(); }