1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17
10
11// <compare>
12
13// template<class T, class U = T> struct compare_three_way_result;
14// template<class T, class U = T>
15// using compare_three_way_result_t = typename compare_three_way_result<T, U>::type;
16
17#include <compare>
18
19#include "test_macros.h"
20
21template<class T>
22concept has_no_nested_type = !requires { typename T::type; };
23
24ASSERT_SAME_TYPE(std::compare_three_way_result_t<int>, std::strong_ordering);
25ASSERT_SAME_TYPE(std::compare_three_way_result_t<float>, std::partial_ordering);
26ASSERT_SAME_TYPE(std::compare_three_way_result_t<unsigned>, std::strong_ordering);
27
28ASSERT_SAME_TYPE(std::compare_three_way_result_t<int, int>, std::strong_ordering);
29ASSERT_SAME_TYPE(std::compare_three_way_result_t<int, float>, std::partial_ordering);
30ASSERT_SAME_TYPE(std::compare_three_way_result_t<float, int>, std::partial_ordering);
31ASSERT_SAME_TYPE(std::compare_three_way_result_t<float, float>, std::partial_ordering);
32ASSERT_SAME_TYPE(std::compare_three_way_result_t<float, unsigned>, std::partial_ordering);
33ASSERT_SAME_TYPE(std::compare_three_way_result_t<unsigned, float>, std::partial_ordering);
34ASSERT_SAME_TYPE(std::compare_three_way_result_t<unsigned, unsigned>, std::strong_ordering);
35
36ASSERT_SAME_TYPE(std::compare_three_way_result_t<const int&>, std::strong_ordering);
37ASSERT_SAME_TYPE(std::compare_three_way_result_t<const int&, int>, std::strong_ordering);
38ASSERT_SAME_TYPE(std::compare_three_way_result_t<const int*>, std::strong_ordering);
39ASSERT_SAME_TYPE(std::compare_three_way_result_t<const int*, int*>, std::strong_ordering);
40
41static_assert(has_no_nested_type<std::compare_three_way_result<void>>);
42static_assert(has_no_nested_type<std::compare_three_way_result<void, void>>);
43static_assert(has_no_nested_type<std::compare_three_way_result<int, void>>);
44static_assert(has_no_nested_type<std::compare_three_way_result<int, int*>>);
45static_assert(has_no_nested_type<std::compare_three_way_result<int, unsigned>>);
46static_assert(has_no_nested_type<std::compare_three_way_result<unsigned, int>>);
47
48struct A {
49 float operator<=>(const A&) const; // a non-comparison-category type is OK
50};
51ASSERT_SAME_TYPE(std::compare_three_way_result_t<A>, float);
52ASSERT_SAME_TYPE(std::compare_three_way_result_t<A, A>, float);
53
54struct B {
55 using T = int(&)();
56 T operator<=>(const B&) const; // no decay takes place either
57};
58ASSERT_SAME_TYPE(std::compare_three_way_result_t<B>, int(&)());
59ASSERT_SAME_TYPE(std::compare_three_way_result_t<B, B>, int(&)());
60
61struct C {
62 std::strong_ordering operator<=>(C&); // C isn't const-comparable
63};
64static_assert(has_no_nested_type<std::compare_three_way_result<C>>);
65static_assert(has_no_nested_type<std::compare_three_way_result<C&>>);
66static_assert(has_no_nested_type<std::compare_three_way_result<C&&>>);
67
68static_assert(has_no_nested_type<std::compare_three_way_result<C, C>>);
69static_assert(has_no_nested_type<std::compare_three_way_result<C&, C&>>);
70static_assert(has_no_nested_type<std::compare_three_way_result<C&&, C&&>>);
71
72struct D {
73 std::strong_ordering operator<=>(D&) &;
74 std::strong_ordering operator<=>(D&&) &&;
75 std::weak_ordering operator<=>(const D&) const&; // comparison is always done by const&
76 std::strong_ordering operator<=>(const D&&) const&&;
77};
78ASSERT_SAME_TYPE(std::compare_three_way_result_t<D>, std::weak_ordering);
79ASSERT_SAME_TYPE(std::compare_three_way_result_t<D&>, std::weak_ordering);
80ASSERT_SAME_TYPE(std::compare_three_way_result_t<D&&>, std::weak_ordering);
81ASSERT_SAME_TYPE(std::compare_three_way_result_t<const D&>, std::weak_ordering);
82ASSERT_SAME_TYPE(std::compare_three_way_result_t<const D&&>, std::weak_ordering);
83
84ASSERT_SAME_TYPE(std::compare_three_way_result_t<D, D>, std::weak_ordering);
85ASSERT_SAME_TYPE(std::compare_three_way_result_t<D&, D&>, std::weak_ordering);
86ASSERT_SAME_TYPE(std::compare_three_way_result_t<D&&, D&&>, std::weak_ordering);
87ASSERT_SAME_TYPE(std::compare_three_way_result_t<const D&, const D&>, std::weak_ordering);
88ASSERT_SAME_TYPE(std::compare_three_way_result_t<const D&&, const D&&>, std::weak_ordering);
89

source code of libcxx/test/std/language.support/cmp/cmp.result/compare_three_way_result.compile.pass.cpp