File: common_reference.compile.pass.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.8-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,388 kB
  • sloc: cpp: 7,438,767; ansic: 1,393,871; asm: 1,012,926; python: 241,728; f90: 86,635; objc: 75,411; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (214 lines) | stat: -rw-r--r-- 10,898 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

// type_traits
// common_reference

#include <tuple>
#include <type_traits>
#include <utility>

#include "test_macros.h"

template <class T>
constexpr bool has_type = requires {
  typename T::type;
};

// A slightly simplified variation of std::tuple
template <class...>
struct UserTuple {};

template <class, class, class>
struct Tuple_helper {};
template <class... Ts, class... Us>
struct Tuple_helper<std::void_t<std::common_reference_t<Ts, Us>...>, UserTuple<Ts...>, UserTuple<Us...> > {
  using type = UserTuple<std::common_reference_t<Ts, Us>...>;
};

template <class... Ts, class... Us, template <class> class TQual, template <class> class UQual>
struct std::basic_common_reference< ::UserTuple<Ts...>, ::UserTuple<Us...>, TQual, UQual>
    : ::Tuple_helper<void, UserTuple<TQual<Ts>...>, UserTuple<UQual<Us>...> > {};

struct X2 {};
struct Y2 {};
struct Z2 {};

template <>
struct std::common_type<X2, Y2> {
  using type = Z2;
};
template <>
struct std::common_type<Y2, X2> {
  using type = Z2;
};

// (6.1)
//  -- If sizeof...(T) is zero, there shall be no member type.
static_assert(!has_type<std::common_reference<> >);

// (6.2)
//  -- Otherwise, if sizeof...(T) is one, let T0 denote the sole type in the
//     pack T. The member typedef type shall denote the same type as T0.
static_assert(std::is_same_v<std::common_reference_t<void>, void>);
static_assert(std::is_same_v<std::common_reference_t<int>, int>);
static_assert(std::is_same_v<std::common_reference_t<int&>, int&>);
static_assert(std::is_same_v<std::common_reference_t<int&&>, int&&>);
static_assert(std::is_same_v<std::common_reference_t<int const>, int const>);
static_assert(std::is_same_v<std::common_reference_t<int const&>, int const&>);
static_assert(std::is_same_v<std::common_reference_t<int const&&>, int const&&>);
static_assert(std::is_same_v<std::common_reference_t<int volatile[]>, int volatile[]>);
static_assert(std::is_same_v<std::common_reference_t<int volatile (&)[]>, int volatile (&)[]>);
static_assert(std::is_same_v<std::common_reference_t<int volatile (&&)[]>, int volatile (&&)[]>);
static_assert(std::is_same_v<std::common_reference_t<void (&)()>, void (&)()>);
static_assert(std::is_same_v<std::common_reference_t<void (&&)()>, void (&&)()>);

// (6.3)
//  -- Otherwise, if sizeof...(T) is two, let T1 and T2 denote the two types in
//     the pack T. Then
// (6.3.1)
//    -- If T1 and T2 are reference types and COMMON_REF(T1, T2) is well-formed,
//       then the member typedef type denotes that type.
struct B {};
struct D : B {};
static_assert(std::is_same_v<std::common_reference_t<B&, D&>, B&>);
static_assert(std::is_same_v<std::common_reference_t<B const&, D&>, B const&>);
static_assert(std::is_same_v<std::common_reference_t<B&, D const&>, B const&>);
static_assert(std::is_same_v<std::common_reference_t<B&, D const&, D&>, B const&>);
static_assert(std::is_same_v<std::common_reference_t<B&, D&, B&, D&>, B&>);

static_assert(std::is_same_v<std::common_reference_t<B&&, D&&>, B&&>);
static_assert(std::is_same_v<std::common_reference_t<B const&&, D&&>, B const&&>);
static_assert(std::is_same_v<std::common_reference_t<B&&, D const&&>, B const&&>);
static_assert(std::is_same_v<std::common_reference_t<B&, D&&>, B const&>);
static_assert(std::is_same_v<std::common_reference_t<B&, D const&&>, B const&>);
static_assert(std::is_same_v<std::common_reference_t<B const&, D&&>, B const&>);

static_assert(std::is_same_v<std::common_reference_t<B&&, D&>, B const&>);
static_assert(std::is_same_v<std::common_reference_t<B&&, D const&>, B const&>);
static_assert(std::is_same_v<std::common_reference_t<B const&&, D&>, B const&>);

static_assert(std::is_same_v<std::common_reference_t<int const&, int volatile&>, int const volatile&>);
static_assert(std::is_same_v<std::common_reference_t<int const volatile&&, int volatile&&>, int const volatile&&>);

static_assert(std::is_same_v<std::common_reference_t<int (&)[10], int (&&)[10]>, int const (&)[10]>);
static_assert(std::is_same_v<std::common_reference_t<int const (&)[10], int volatile (&)[10]>, int const volatile (&)[10]>);

// (6.3.2)
//    -- Otherwise, if basic_common_reference<remove_cvref_t<T1>,
//       remove_cvref_t<T2>, XREF(T1), XREF(T2)>::type is well-formed, then the
//       member typedef type denotes that type.
static_assert(std::is_same_v<std::common_reference_t<const UserTuple<int, short>&, UserTuple<int&, short volatile&>>,
                             UserTuple<const int&, const volatile short&>>);

static_assert(std::is_same_v<std::common_reference_t<volatile UserTuple<int, short>&, const UserTuple<int, short>&>,
                             const volatile UserTuple<int, short>&>);

// (6.3.3)
//    -- Otherwise, if COND_RES(T1, T2) is well-formed, then the member typedef
//       type denotes that type.
static_assert(std::is_same_v<std::common_reference_t<void, void>, void>);
static_assert(std::is_same_v<std::common_reference_t<int, short>, int>);
static_assert(std::is_same_v<std::common_reference_t<int, short&>, int>);
static_assert(std::is_same_v<std::common_reference_t<int&, short&>, int>);
static_assert(std::is_same_v<std::common_reference_t<int&, short>, int>);

// tricky volatile reference case
static_assert(std::is_same_v<std::common_reference_t<int&&, int volatile&>, int>);
static_assert(std::is_same_v<std::common_reference_t<int volatile&, int&&>, int>);

static_assert(std::is_same_v<std::common_reference_t<int (&)[10], int (&)[11]>, int*>);

// https://github.com/ericniebler/stl2/issues/338
struct MyIntRef {
  MyIntRef(int&);
};
static_assert(std::is_same_v<std::common_reference_t<int&, MyIntRef>, MyIntRef>);

// (6.3.4)
//    -- Otherwise, if common_type_t<T1, T2> is well-formed, then the member
//       typedef type denotes that type.
struct moveonly {
  moveonly() = default;
  moveonly(moveonly&&) = default;
  moveonly& operator=(moveonly&&) = default;
};
struct moveonly2 : moveonly {};

static_assert(std::is_same_v<std::common_reference_t<moveonly const&, moveonly>, moveonly>);
static_assert(std::is_same_v<std::common_reference_t<moveonly2 const&, moveonly>, moveonly>);
static_assert(std::is_same_v<std::common_reference_t<moveonly const&, moveonly2>, moveonly>);

static_assert(std::is_same_v<std::common_reference_t<X2&, Y2 const&>, Z2>);

// (6.3.5)
//    -- Otherwise, there shall be no member type.
static_assert(!has_type<std::common_reference<volatile UserTuple<short>&, const UserTuple<int, short>&> >);

// (6.4)
//  -- Otherwise, if sizeof...(T) is greater than two, let T1, T2, and Rest,
//     respectively, denote the first, second, and (pack of) remaining types
//     comprising T. Let C be the type common_reference_t<T1, T2>. Then:
// (6.4.1)
//    -- If there is such a type C, the member typedef type shall denote the
//       same type, if any, as common_reference_t<C, Rest...>.
static_assert(std::is_same_v<std::common_reference_t<int, int, int>, int>);
static_assert(std::is_same_v<std::common_reference_t<int&&, int const&, int volatile&>, int const volatile&>);
static_assert(std::is_same_v<std::common_reference_t<int&&, int const&, float&>, float>);

// (6.4.2)
//    -- Otherwise, there shall be no member type.
static_assert(!has_type<std::common_reference<int, short, int, char*> >);

#if TEST_STD_VER > 20
static_assert(std::is_same_v<std::common_reference_t<std::tuple<int, int>>, std::tuple<int, int>>);
static_assert(std::is_same_v<std::common_reference_t<std::tuple<int, long>, std::tuple<long, int>>, std::tuple<long, long>>);
static_assert(std::is_same_v<std::common_reference_t<std::tuple<int&, const int&>, std::tuple<const int&, int>>,
                             std::tuple<const int&, int>>);
static_assert(std::is_same_v<std::common_reference_t<std::tuple<int&, volatile int&>, std::tuple<volatile int&, int>>,
                             std::tuple<volatile int&, int>>);
static_assert(std::is_same_v<std::common_reference_t<std::tuple<int&, const volatile int&>, std::tuple<const volatile int&, int>>,
                             std::tuple<const volatile int&, int>>);
static_assert(!has_type<std::common_reference_t<std::tuple<const int&, volatile int&>, std::tuple<volatile int&, const int&>>>);

static_assert(std::is_same_v<std::common_reference_t<std::tuple<int, X2>, std::tuple<int, Y2>>, std::tuple<int, Z2>>);
static_assert(std::is_same_v<std::common_reference_t<std::tuple<int, X2>, std::tuple<int, Y2>>, std::tuple<int, Z2>>);
static_assert(!has_type<std::common_reference<std::tuple<int, const X2>, std::tuple<float, const Z2>>>);
static_assert(!has_type<std::common_reference<std::tuple<int, X2>, std::tuple<float, Z2>>>);
static_assert(!has_type<std::common_reference<std::tuple<int, X2>, int, X2>>);

struct A {};
template <template<class> class TQual, template<class> class UQual>
struct std::basic_common_reference<A, std::tuple<B>, TQual, UQual> {
  using type = tuple<UQual<B>>;
};

static_assert(std::is_same_v<std::common_reference_t<A, std::tuple<B>, std::tuple<D>>, std::tuple<B>>);


static_assert(std::is_same_v<std::common_reference_t<std::pair<int, int>>,
                             std::pair<int, int>>);
static_assert(std::is_same_v<std::common_reference_t<std::pair<int, long>, std::pair<long, int>>,
                             std::pair<long, long>>);
static_assert(std::is_same_v<std::common_reference_t<std::pair<int&, const int&>, std::pair<const int&, int>>,
                             std::pair<const int&, int>>);
static_assert(std::is_same_v<std::common_reference_t<std::pair<int&, volatile int&>, std::pair<volatile int&, int>>,
                             std::pair<volatile int&, int>>);
static_assert(std::is_same_v<std::common_reference_t<std::pair<int&, const volatile int&>, std::pair<const volatile int&, int>>,
                             std::pair<const volatile int&, int>>);
static_assert(!has_type<std::common_reference_t<std::pair<const int&, volatile int&>,
                        std::pair<volatile int&, const int&>>>);

static_assert(std::is_same_v<std::common_reference_t<std::pair<int, X2>, std::pair<int, Y2>>, std::pair<int, Z2>>);
static_assert(std::is_same_v<std::common_reference_t<std::pair<int, X2>, std::pair<int, Y2>>, std::pair<int, Z2>>);
static_assert(!has_type<std::common_reference<std::pair<int, const X2>, std::pair<float, const Z2>>>);
static_assert(!has_type<std::common_reference<std::pair<int, X2>, std::pair<float, Z2>>>);
static_assert(!has_type<std::common_reference<std::pair<int, X2>, int, X2>>);
#endif