| 1 |
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=c++17 -Wno-vla-cxx-extension %s |
| 2 |
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=c++20 -Wno-vla-cxx-extension %s |
| 3 |
|
| 4 |
#if !__has_builtin(__builtin_common_type) |
| 5 |
# error |
| 6 |
#endif |
| 7 |
|
| 8 |
// expected-note@*:* {{template declaration from hidden source: template <template <class ...> class, template <class> class, class, class ...>}} |
| 9 |
|
| 10 |
void test() { |
| 11 |
__builtin_common_type<> a; // expected-error {{too few template arguments for template '__builtin_common_type'}} |
| 12 |
__builtin_common_type<1> b; // expected-error {{template argument for template template parameter must be a class template or type alias template}} |
| 13 |
__builtin_common_type<int, 1> c; // expected-error {{template argument for template template parameter must be a class template or type alias template}} |
| 14 |
} |
| 15 |
|
| 16 |
struct empty_type {}; |
| 17 |
|
| 18 |
template <class T> |
| 19 |
struct type_identity { |
| 20 |
using type = T; |
| 21 |
}; |
| 22 |
|
| 23 |
template <class...> |
| 24 |
struct common_type; |
| 25 |
|
| 26 |
template <class... Args> |
| 27 |
using common_type_t = typename common_type<Args...>::type; |
| 28 |
|
| 29 |
void test_vla() { |
| 30 |
int i = 4; |
| 31 |
int VLA[i]; |
| 32 |
__builtin_common_type<common_type_t, type_identity, empty_type, decltype(VLA)> d; // expected-error {{variably modified type 'decltype(VLA)' (aka 'int[i]') cannot be used as a template argument}} |
| 33 |
} |
| 34 |
|
| 35 |
template <class... Args> |
| 36 |
using common_type_base = __builtin_common_type<common_type_t, type_identity, empty_type, Args...>; |
| 37 |
// expected-note@-1 {{in instantiation of default function argument expression for 'InvalidConversion<void>' required here}} |
| 38 |
|
| 39 |
template <class... Args> |
| 40 |
struct common_type : common_type_base<Args...> {}; |
| 41 |
|
| 42 |
struct Incomplete; |
| 43 |
|
| 44 |
template<> |
| 45 |
struct common_type<Incomplete, Incomplete>; |
| 46 |
|
| 47 |
static_assert(__is_same(common_type_base<>, empty_type)); |
| 48 |
static_assert(__is_same(common_type_base<Incomplete>, empty_type)); |
| 49 |
static_assert(__is_same(common_type_base<char>, type_identity<char>)); |
| 50 |
static_assert(__is_same(common_type_base<int>, type_identity<int>)); |
| 51 |
static_assert(__is_same(common_type_base<const int>, type_identity<int>)); |
| 52 |
static_assert(__is_same(common_type_base<volatile int>, type_identity<int>)); |
| 53 |
static_assert(__is_same(common_type_base<const volatile int>, type_identity<int>)); |
| 54 |
static_assert(__is_same(common_type_base<int[]>, type_identity<int*>)); |
| 55 |
static_assert(__is_same(common_type_base<const int[]>, type_identity<const int*>)); |
| 56 |
static_assert(__is_same(common_type_base<void(&)()>, type_identity<void(*)()>)); |
| 57 |
static_assert(__is_same(common_type_base<int[], int[]>, type_identity<int*>)); |
| 58 |
|
| 59 |
static_assert(__is_same(common_type_base<int, int>, type_identity<int>)); |
| 60 |
static_assert(__is_same(common_type_base<int, long>, type_identity<long>)); |
| 61 |
static_assert(__is_same(common_type_base<long, int>, type_identity<long>)); |
| 62 |
static_assert(__is_same(common_type_base<long, long>, type_identity<long>)); |
| 63 |
|
| 64 |
static_assert(__is_same(common_type_base<const int, long>, type_identity<long>)); |
| 65 |
static_assert(__is_same(common_type_base<const volatile int, long>, type_identity<long>)); |
| 66 |
static_assert(__is_same(common_type_base<int, const long>, type_identity<long>)); |
| 67 |
static_assert(__is_same(common_type_base<int, const volatile long>, type_identity<long>)); |
| 68 |
|
| 69 |
static_assert(__is_same(common_type_base<int*, long*>, empty_type)); |
| 70 |
|
| 71 |
static_assert(__is_same(common_type_base<int, long, float>, type_identity<float>)); |
| 72 |
static_assert(__is_same(common_type_base<unsigned, char, long>, type_identity<long>)); |
| 73 |
static_assert(__is_same(common_type_base<long long, long long, long>, type_identity<long long>)); |
| 74 |
|
| 75 |
static_assert(__is_same(common_type_base<int [[clang::address_space(1)]]>, type_identity<int [[clang::address_space(1)]]>)); |
| 76 |
static_assert(__is_same(common_type_base<int [[clang::address_space(1)]], int>, type_identity<int>)); |
| 77 |
static_assert(__is_same(common_type_base<long [[clang::address_space(1)]], int>, type_identity<long>)); |
| 78 |
static_assert(__is_same(common_type_base<long [[clang::address_space(1)]], int [[clang::address_space(1)]]>, type_identity<long>)); |
| 79 |
static_assert(__is_same(common_type_base<long [[clang::address_space(1)]], long [[clang::address_space(1)]]>, type_identity<long [[clang::address_space(1)]]>)); |
| 80 |
static_assert(__is_same(common_type_base<long [[clang::address_space(1)]], long [[clang::address_space(2)]]>, type_identity<long>)); |
| 81 |
|
| 82 |
struct S {}; |
| 83 |
struct T : S {}; |
| 84 |
|
| 85 |
static_assert(__is_same(common_type_base<int S::*, int S::*>, type_identity<int S::*>)); |
| 86 |
static_assert(__is_same(common_type_base<int S::*, int T::*>, type_identity<int T::*>)); |
| 87 |
static_assert(__is_same(common_type_base<int S::*, long S::*>, empty_type)); |
| 88 |
|
| 89 |
static_assert(__is_same(common_type_base<int (S::*)(), int (S::*)()>, type_identity<int (S::*)()>)); |
| 90 |
static_assert(__is_same(common_type_base<int (S::*)(), int (T::*)()>, type_identity<int (T::*)()>)); |
| 91 |
static_assert(__is_same(common_type_base<int (S::*)(), long (S::*)()>, empty_type)); |
| 92 |
|
| 93 |
struct NoCommonType {}; |
| 94 |
|
| 95 |
template <> |
| 96 |
struct common_type<NoCommonType, NoCommonType> {}; |
| 97 |
|
| 98 |
struct CommonTypeInt {}; |
| 99 |
|
| 100 |
template <> |
| 101 |
struct common_type<CommonTypeInt, CommonTypeInt> { |
| 102 |
using type = int; |
| 103 |
}; |
| 104 |
|
| 105 |
template <> |
| 106 |
struct common_type<CommonTypeInt, int> { |
| 107 |
using type = int; |
| 108 |
}; |
| 109 |
|
| 110 |
template <> |
| 111 |
struct common_type<int, CommonTypeInt> { |
| 112 |
using type = int; |
| 113 |
}; |
| 114 |
|
| 115 |
static_assert(__is_same(common_type_base<NoCommonType>, empty_type)); |
| 116 |
static_assert(__is_same(common_type_base<CommonTypeInt>, type_identity<int>)); |
| 117 |
static_assert(__is_same(common_type_base<NoCommonType, NoCommonType, NoCommonType>, empty_type)); |
| 118 |
static_assert(__is_same(common_type_base<CommonTypeInt, CommonTypeInt, CommonTypeInt>, type_identity<int>)); |
| 119 |
static_assert(__is_same(common_type_base<CommonTypeInt&, CommonTypeInt&&>, type_identity<int>)); |
| 120 |
|
| 121 |
static_assert(__is_same(common_type_base<void, int>, empty_type)); |
| 122 |
static_assert(__is_same(common_type_base<void, void>, type_identity<void>)); |
| 123 |
static_assert(__is_same(common_type_base<const void, void>, type_identity<void>)); |
| 124 |
static_assert(__is_same(common_type_base<void, const void>, type_identity<void>)); |
| 125 |
|
| 126 |
template <class T> |
| 127 |
struct ConvertibleTo { |
| 128 |
operator T(); |
| 129 |
}; |
| 130 |
|
| 131 |
static_assert(__is_same(common_type_base<ConvertibleTo<int>>, type_identity<ConvertibleTo<int>>)); |
| 132 |
static_assert(__is_same(common_type_base<ConvertibleTo<int>, int>, type_identity<int>)); |
| 133 |
static_assert(__is_same(common_type_base<ConvertibleTo<int&>, ConvertibleTo<long&>>, type_identity<long>)); |
| 134 |
|
| 135 |
struct ConvertibleToB; |
| 136 |
|
| 137 |
struct ConvertibleToA { |
| 138 |
operator ConvertibleToB(); |
| 139 |
}; |
| 140 |
|
| 141 |
struct ConvertibleToB { |
| 142 |
operator ConvertibleToA(); |
| 143 |
}; |
| 144 |
|
| 145 |
static_assert(__is_same(common_type_base<ConvertibleToA, ConvertibleToB>, empty_type)); |
| 146 |
|
| 147 |
struct const_ref_convertible { |
| 148 |
operator int&() const &; |
| 149 |
operator int&() && = delete; |
| 150 |
}; |
| 151 |
|
| 152 |
#if __cplusplus >= 202002L |
| 153 |
static_assert(__is_same(common_type_base<const_ref_convertible, int &>, type_identity<int>)); |
| 154 |
#else |
| 155 |
static_assert(__is_same(common_type_base<const_ref_convertible, int &>, empty_type)); |
| 156 |
#endif |
| 157 |
|
| 158 |
struct WeirdConvertible_1p2_p3 {}; |
| 159 |
|
| 160 |
struct WeirdConvertible3 { |
| 161 |
operator WeirdConvertible_1p2_p3(); |
| 162 |
}; |
| 163 |
|
| 164 |
struct WeirdConvertible1p2 { |
| 165 |
operator WeirdConvertible_1p2_p3(); |
| 166 |
}; |
| 167 |
|
| 168 |
template <> |
| 169 |
struct common_type<WeirdConvertible3, WeirdConvertible1p2> { |
| 170 |
using type = WeirdConvertible_1p2_p3; |
| 171 |
}; |
| 172 |
|
| 173 |
template <> |
| 174 |
struct common_type<WeirdConvertible1p2, WeirdConvertible3> { |
| 175 |
using type = WeirdConvertible_1p2_p3; |
| 176 |
}; |
| 177 |
|
| 178 |
struct WeirdConvertible1 { |
| 179 |
operator WeirdConvertible1p2(); |
| 180 |
}; |
| 181 |
|
| 182 |
struct WeirdConvertible2 { |
| 183 |
operator WeirdConvertible1p2(); |
| 184 |
}; |
| 185 |
|
| 186 |
template <> |
| 187 |
struct common_type<WeirdConvertible1, WeirdConvertible2> { |
| 188 |
using type = WeirdConvertible1p2; |
| 189 |
}; |
| 190 |
|
| 191 |
template <> |
| 192 |
struct common_type<WeirdConvertible2, WeirdConvertible1> { |
| 193 |
using type = WeirdConvertible1p2; |
| 194 |
}; |
| 195 |
|
| 196 |
static_assert(__is_same(common_type_base<WeirdConvertible1, WeirdConvertible2, WeirdConvertible3>, |
| 197 |
type_identity<WeirdConvertible_1p2_p3>)); |
| 198 |
|
| 199 |
struct PrivateTypeMember |
| 200 |
{ |
| 201 |
operator int(); |
| 202 |
}; |
| 203 |
|
| 204 |
template<> |
| 205 |
struct common_type<PrivateTypeMember, PrivateTypeMember> |
| 206 |
{ |
| 207 |
private: |
| 208 |
using type = int; |
| 209 |
}; |
| 210 |
|
| 211 |
static_assert(__is_same(common_type_base<PrivateTypeMember, PrivateTypeMember, PrivateTypeMember>, empty_type)); |
| 212 |
|
| 213 |
class PrivateConstructor { |
| 214 |
private: |
| 215 |
PrivateConstructor(int); |
| 216 |
}; |
| 217 |
|
| 218 |
static_assert(__is_same(common_type_base<int, PrivateConstructor>, empty_type)); |
| 219 |
|
| 220 |
// expected-note@+1 {{in instantiation of template type alias 'common_type_base' requested here}} |
| 221 |
template<typename A, typename B, typename Res = common_type_base<A, B>> |
| 222 |
static Res common_type_sfinae(); |
| 223 |
// expected-note@-1 {{in instantiation of default argument for 'common_type_sfinae<int, InvalidConversion>' required here}} |
| 224 |
|
| 225 |
// Make sure we don't emit "calling a private constructor" in SFINAE context ... |
| 226 |
static_assert(__is_same(decltype(common_type_sfinae<int, PrivateConstructor>()), empty_type)); |
| 227 |
|
| 228 |
// ... but we still emit errors outside of the immediate context. |
| 229 |
template<typename T> |
| 230 |
struct Member { |
| 231 |
T t; // expected-error {{field has incomplete type 'void'}} |
| 232 |
}; |
| 233 |
|
| 234 |
// The conversion from int has a non-SFINAE error. |
| 235 |
class InvalidConversion { |
| 236 |
private: |
| 237 |
template<typename T = void> |
| 238 |
InvalidConversion(int, Member<T> = {}); |
| 239 |
// expected-note@-1 {{in instantiation of template class 'Member<void>' requested here}} |
| 240 |
// expected-note@-2 {{passing argument to parameter here}} |
| 241 |
}; |
| 242 |
|
| 243 |
// expected-note@+1 {{while substituting deduced template arguments into function template 'common_type_sfinae'}} |
| 244 |
static_assert(__is_same(decltype(common_type_sfinae<int, InvalidConversion>()), empty_type)); |