1 // RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify=expected -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify=expected -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify=expected,cxx14_17 -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify=expected,cxx17 -fexceptions -fcxx-exceptions -pedantic-errors
6 namespace dr1512
{ // dr1512: 4
8 if (p
> 0) {} // expected-error {{ordered comparison between pointer and zero}}
9 #if __cplusplus >= 201103L
10 if (p
> nullptr) {} // expected-error {{invalid operands}}
13 bool g(int **x
, const int **y
) {
17 template<typename T
> T
val();
19 template<typename A
, typename B
, typename C
> void composite_pointer_type_is_base() {
20 typedef __typeof(true ? val
<A
>() : val
<B
>()) type
;
23 typedef __typeof(val
<A
>() == val
<B
>()) cmp
;
24 typedef __typeof(val
<A
>() != val
<B
>()) cmp
;
28 template<typename A
, typename B
, typename C
> void composite_pointer_type_is_ord() {
29 composite_pointer_type_is_base
<A
, B
, C
>();
31 typedef __typeof(val
<A
>() < val
<B
>()) cmp
; // cxx17-warning 2 {{ordered comparison of function pointers}}
32 typedef __typeof(val
<A
>() <= val
<B
>()) cmp
; // cxx17-warning 2 {{ordered comparison of function pointers}}
33 typedef __typeof(val
<A
>() > val
<B
>()) cmp
; // cxx17-warning 2 {{ordered comparison of function pointers}}
34 typedef __typeof(val
<A
>() >= val
<B
>()) cmp
; // cxx17-warning 2 {{ordered comparison of function pointers}}
38 template <typename A
, typename B
, typename C
>
39 void composite_pointer_type_is_unord(int = 0) {
40 composite_pointer_type_is_base
<A
, B
, C
>();
42 template <typename A
, typename B
, typename C
>
43 void composite_pointer_type_is_unord(__typeof(val
<A
>() < val
<B
>()) * = 0);
44 template <typename A
, typename B
, typename C
>
45 void composite_pointer_type_is_unord(__typeof(val
<A
>() <= val
<B
>()) * = 0);
46 template <typename A
, typename B
, typename C
>
47 void composite_pointer_type_is_unord(__typeof(val
<A
>() > val
<B
>()) * = 0);
48 template <typename A
, typename B
, typename C
>
49 void composite_pointer_type_is_unord(__typeof(val
<A
>() >= val
<B
>()) * = 0);
51 // A call to this is ambiguous if a composite pointer type exists.
52 template<typename A
, typename B
>
53 void no_composite_pointer_type(__typeof((true ? val
<A
>() : val
<B
>()), void()) * = 0);
54 template<typename A
, typename B
> void no_composite_pointer_type(int = 0);
61 #if __cplusplus >= 201103L
62 using nullptr_t
= decltype(nullptr);
63 composite_pointer_type_is_unord
<nullptr_t
, nullptr_t
, nullptr_t
>();
64 no_composite_pointer_type
<nullptr_t
, int>();
66 composite_pointer_type_is_unord
<nullptr_t
, const char**, const char**>();
67 composite_pointer_type_is_unord
<const char**, nullptr_t
, const char**>();
70 composite_pointer_type_is_ord
<const int *, volatile void *, const volatile void*>();
71 composite_pointer_type_is_ord
<const void *, volatile int *, const volatile void*>();
73 composite_pointer_type_is_ord
<const A
*, volatile B
*, const volatile A
*>();
74 composite_pointer_type_is_ord
<const B
*, volatile A
*, const volatile A
*>();
76 composite_pointer_type_is_unord
<const int *A::*, volatile int *B::*, const volatile int *const B::*>();
77 composite_pointer_type_is_unord
<const int *B::*, volatile int *A::*, const volatile int *const B::*>();
78 no_composite_pointer_type
<int (A::*)(), int (C::*)()>();
79 no_composite_pointer_type
<const int (A::*)(), volatile int (C::*)()>();
81 #if __cplusplus > 201402
82 composite_pointer_type_is_ord
<int (*)() noexcept
, int (*)(), int (*)()>(); // expected-note {{requested here}}
83 composite_pointer_type_is_ord
<int (*)(), int (*)() noexcept
, int (*)()>(); // expected-note {{requested here}}
84 composite_pointer_type_is_unord
<int (A::*)() noexcept
, int (A::*)(), int (A::*)()>();
85 composite_pointer_type_is_unord
<int (A::*)(), int (A::*)() noexcept
, int (A::*)()>();
86 // FIXME: This looks like a standard defect; these should probably all have type 'int (B::*)()'.
87 composite_pointer_type_is_unord
<int (B::*)(), int (A::*)() noexcept
, int (B::*)()>();
88 composite_pointer_type_is_unord
<int (A::*)() noexcept
, int (B::*)(), int (B::*)()>();
89 composite_pointer_type_is_unord
<int (B::*)() noexcept
, int (A::*)(), int (B::*)()>();
90 composite_pointer_type_is_unord
<int (A::*)(), int (B::*)() noexcept
, int (B::*)()>();
92 // FIXME: It would be reasonable to permit these, with a common type of 'int (*const *)()'.
93 no_composite_pointer_type
<int (**)() noexcept
, int (**)()>();
94 no_composite_pointer_type
<int (**)(), int (**)() noexcept
>();
96 // FIXME: It would be reasonable to permit these, with a common type of 'int (A::*)()'.
97 no_composite_pointer_type
<int (A::*)() const, int (A::*)()>();
98 no_composite_pointer_type
<int (A::*)(), int (A::*)() const>();
100 // FIXME: It would be reasonable to permit these, with a common type of
101 // 'int (A::*)() &' and 'int (A::*)() &&', respectively.
102 no_composite_pointer_type
<int (A::*)() &, int (A::*)()>();
103 no_composite_pointer_type
<int (A::*)(), int (A::*)() &>();
104 no_composite_pointer_type
<int (A::*)() &&, int (A::*)()>();
105 no_composite_pointer_type
<int (A::*)(), int (A::*)() &&>();
107 no_composite_pointer_type
<int (A::*)() &&, int (A::*)() &>();
108 no_composite_pointer_type
<int (A::*)() &, int (A::*)() &&>();
110 no_composite_pointer_type
<int (C::*)(), int (A::*)() noexcept
>();
111 no_composite_pointer_type
<int (A::*)() noexcept
, int (C::*)()>();
115 #if __cplusplus >= 201103L
116 template<typename T
> struct Wrap
{ operator T(); }; // expected-note 4{{converted to type 'std::nullptr_t'}} expected-note 4{{converted to type 'int *'}}
117 void test_overload() {
118 using nullptr_t
= decltype(nullptr);
119 void(Wrap
<nullptr_t
>() == Wrap
<nullptr_t
>());
120 void(Wrap
<nullptr_t
>() != Wrap
<nullptr_t
>());
121 void(Wrap
<nullptr_t
>() < Wrap
<nullptr_t
>()); // expected-error {{invalid operands}}
122 void(Wrap
<nullptr_t
>() > Wrap
<nullptr_t
>()); // expected-error {{invalid operands}}
123 void(Wrap
<nullptr_t
>() <= Wrap
<nullptr_t
>()); // expected-error {{invalid operands}}
124 void(Wrap
<nullptr_t
>() >= Wrap
<nullptr_t
>()); // expected-error {{invalid operands}}
126 // Under dr1213, this is ill-formed: we select the builtin operator<(int*, int*)
127 // but then only convert as far as 'nullptr_t', which we then can't convert to 'int*'.
128 void(Wrap
<nullptr_t
>() == Wrap
<int*>());
129 void(Wrap
<nullptr_t
>() != Wrap
<int*>());
130 void(Wrap
<nullptr_t
>() < Wrap
<int*>()); // expected-error {{invalid operands to binary expression ('Wrap<nullptr_t>' (aka 'Wrap<std::nullptr_t>') and 'Wrap<int *>')}}
131 void(Wrap
<nullptr_t
>() > Wrap
<int*>()); // expected-error {{invalid operands}}
132 void(Wrap
<nullptr_t
>() <= Wrap
<int*>()); // expected-error {{invalid operands}}
133 void(Wrap
<nullptr_t
>() >= Wrap
<int*>()); // expected-error {{invalid operands}}
138 namespace dr1514
{ // dr1514: 11
139 #if __cplusplus >= 201103L
141 enum E
: int {}; // expected-note {{previous}}
142 enum E
: int {}; // expected-error {{redefinition}}
144 S::E se
; // OK, complete type, not zero-width bitfield.
146 // The behavior in other contexts is superseded by DR1966.
150 namespace dr1518
{ // dr1518: 4
151 #if __cplusplus >= 201103L
152 struct Z0
{ // expected-note 0+ {{candidate}}
153 explicit Z0() = default; // expected-note 0+ {{here}}
155 struct Z
{ // expected-note 0+ {{candidate}}
156 explicit Z(); // expected-note 0+ {{here}}
157 explicit Z(int); // expected-note {{not a candidate}}
158 explicit Z(int, int); // expected-note 0+ {{here}}
160 template <class T
> int Eat(T
); // expected-note 0+ {{candidate}}
163 Z0 c
= {}; // expected-error {{explicit in copy-initialization}}
164 int i
= Eat
<Z0
>({}); // expected-error {{no matching function for call to 'Eat'}}
166 Z c2
= {}; // expected-error {{explicit in copy-initialization}}
167 int i2
= Eat
<Z
>({}); // expected-error {{no matching function for call to 'Eat'}}
168 Z a1
= 1; // expected-error {{no viable conversion}}
173 Z a5
= static_cast<Z
>(1);
174 Z a6
= {4, 3}; // expected-error {{explicit in copy-initialization}}
176 struct UserProvidedBaseCtor
{ // expected-note 0+ {{candidate}}
177 UserProvidedBaseCtor() {}
179 struct DoesntInheritCtor
: UserProvidedBaseCtor
{ // expected-note 0+ {{candidate}}
182 DoesntInheritCtor I
{{}, 42};
183 #if __cplusplus <= 201402L
184 // expected-error@-2 {{no matching constructor}}
187 struct BaseCtor
{ BaseCtor() = default; }; // expected-note 0+ {{candidate}}
188 struct InheritsCtor
: BaseCtor
{ // expected-note 1+ {{candidate}}
189 using BaseCtor::BaseCtor
; // expected-note 2 {{inherited here}}
192 InheritsCtor II
= {{}, 42}; // expected-error {{no matching constructor}}
194 namespace std_example
{
196 explicit A() = default; // expected-note 2{{declared here}}
200 explicit B() = default; // expected-note 2{{declared here}}
204 explicit C(); // expected-note 2{{declared here}}
209 explicit D() = default; // expected-note 2{{declared here}}
212 template <typename T
> void f() {
215 T v
= {}; // expected-error 4{{explicit}}
217 template <typename T
> void g() {
218 void x(T t
); // expected-note 4{{parameter}}
219 x({}); // expected-error 4{{explicit}}
223 f
<A
>(); // expected-note {{instantiation of}}
224 f
<B
>(); // expected-note {{instantiation of}}
225 f
<C
>(); // expected-note {{instantiation of}}
226 f
<D
>(); // expected-note {{instantiation of}}
227 g
<A
>(); // expected-note {{instantiation of}}
228 g
<B
>(); // expected-note {{instantiation of}}
229 g
<C
>(); // expected-note {{instantiation of}}
230 g
<D
>(); // expected-note {{instantiation of}}
233 #endif // __cplusplus >= 201103L
236 namespace dr1550
{ // dr1550: yes
237 int f(bool b
, int n
) {
238 return (b
? (throw 0) : n
) + (b
? n
: (throw 0));
242 namespace dr1558
{ // dr1558: 12
243 #if __cplusplus >= 201103L
244 template<class T
, class...> using first_of
= T
;
245 template<class T
> first_of
<void, typename
T::type
> f(int); // expected-note {{'int' cannot be used prior to '::'}}
246 template<class T
> void f(...) = delete; // expected-note {{deleted}}
248 struct X
{ typedef void type
; };
251 f
<int>(0); // expected-error {{deleted}}
256 namespace dr1560
{ // dr1560: 3.5
257 void f(bool b
, int n
) {
258 (b
? throw 0 : n
) = (b
? n
: throw 0) = 0;
260 class X
{ X(const X
&); };
262 const X
&x
= true ? get() : throw 0;
265 namespace dr1563
{ // dr1563: yes
266 #if __cplusplus >= 201103L
267 double bar(double) { return 0.0; }
268 float bar(float) { return 0.0f
; }
270 using fun
= double(double);
275 namespace dr1573
{ // dr1573: 3.9
276 #if __cplusplus >= 201103L
277 // ellipsis is inherited (p0136r1 supersedes this part).
278 struct A
{ A(); A(int, char, ...); };
279 struct B
: A
{ using A::A
; };
280 B
b(1, 'x', 4.0, "hello"); // ok
282 // inherited constructor is effectively constexpr if the user-written constructor would be
283 struct C
{ C(); constexpr C(int) {} };
284 struct D
: C
{ using C::C
; };
285 constexpr D d
= D(0); // ok
286 struct E
: C
{ using C::C
; A a
; }; // expected-note {{non-literal type}}
287 constexpr E e
= E(0); // expected-error {{non-literal type}}
288 // FIXME: This diagnostic is pretty bad; we should explain that the problem
289 // is that F::c would be initialized by a non-constexpr constructor.
290 struct F
: C
{ using C::C
; C c
; }; // expected-note {{here}}
291 constexpr F f
= F(0); // expected-error {{constant expression}} expected-note {{constructor inherited from base class 'C'}}
293 // inherited constructor is effectively deleted if the user-written constructor would be
294 struct G
{ G(int); };
295 struct H
: G
{ using G::G
; G g
; }; // expected-note {{constructor inherited by 'H' is implicitly deleted because field 'g' has no default constructor}}
296 H
h(0); // expected-error {{constructor inherited by 'H' from base class 'G' is implicitly deleted}}
300 #if __cplusplus >= 201103L
302 typedef decltype(sizeof(int)) size_t;
304 // libc++'s implementation
306 class initializer_list
311 initializer_list(const _E
* __b
, size_t __s
)
312 : __begin_(__b
), __size_(__s
) {}
315 typedef _E value_type
;
316 typedef const _E
& reference
;
317 typedef const _E
& const_reference
;
318 typedef size_t size_type
;
320 typedef const _E
* iterator
;
321 typedef const _E
* const_iterator
;
323 initializer_list() : __begin_(nullptr), __size_(0) {}
325 size_t size() const {return __size_
;}
326 const _E
* begin() const {return __begin_
;}
327 const _E
* end() const {return __begin_
+ __size_
;}
330 template < class _T1
, class _T2
> struct pair
{ _T2 second
; };
332 template<typename T
> struct basic_string
{
333 basic_string(const T
* x
) {}
336 typedef basic_string
<char> string
;
340 namespace dr1579
{ // dr1579: 3.9
342 struct GenericMoveOnly
{
344 template<class U
> GenericMoveOnly(const GenericMoveOnly
<U
> &) = delete; // expected-note 5 {{marked deleted here}}
345 GenericMoveOnly(const int &) = delete; // expected-note 2 {{marked deleted here}}
346 template<class U
> GenericMoveOnly(GenericMoveOnly
<U
> &&);
347 GenericMoveOnly(int &&);
350 GenericMoveOnly
<float> DR1579_Eligible(GenericMoveOnly
<char> CharMO
) {
352 GenericMoveOnly
<char> GMO
;
364 GenericMoveOnly
<char> GlobalMO
;
366 GenericMoveOnly
<float> DR1579_Ineligible(int &AnInt
,
367 GenericMoveOnly
<char> &CharMO
) {
368 static GenericMoveOnly
<char> StaticMove
;
369 extern GenericMoveOnly
<char> ExternMove
;
372 return AnInt
; // expected-error{{invokes a deleted function}}
374 return GlobalMO
; // expected-error{{invokes a deleted function}}
376 return StaticMove
; // expected-error{{invokes a deleted function}}
378 return ExternMove
; // expected-error{{invokes a deleted function}}
380 return AnInt
; // expected-error{{invokes a deleted function}}
382 return CharMO
; // expected-error{{invokes a deleted function}}
385 auto DR1579_lambda_valid
= [](GenericMoveOnly
<float> mo
) ->
386 GenericMoveOnly
<char> {
390 auto DR1579_lambda_invalid
= []() -> GenericMoveOnly
<char> {
391 static GenericMoveOnly
<float> mo
;
392 return mo
; // expected-error{{invokes a deleted function}}
394 } // end namespace dr1579
397 // Deducing function types from cv-qualified types
398 template<typename T
> void f(const T
*); // expected-note {{candidate template ignored}}
399 template<typename T
> void g(T
*, const T
* = 0);
400 template<typename T
> void h(T
*) { T::error
; } // expected-error {{no members}}
401 template<typename T
> void h(const T
*);
403 f(&i
); // expected-error {{no matching function}}
405 h(&i
); // expected-note {{here}}
409 namespace dr1589
{ // dr1589: 3.7 c++11
410 // Ambiguous ranking of list-initialization sequences
412 void f0(long, int=0); // Would makes selection of #0 ambiguous
414 void f0(std::initializer_list
<int>); // #00
415 void g0() { f0({1L}); } // chooses #00
417 void f1(int, int=0); // Would make selection of #1 ambiguous
419 void f1(std::initializer_list
<long>); // #2
420 void g1() { f1({42}); } // chooses #2
422 void f2(std::pair
<const char*, const char*>, int = 0); // Would makes selection of #3 ambiguous
423 void f2(std::pair
<const char*, const char*>); // #3
424 void f2(std::initializer_list
<std::string
>); // #4
425 void g2() { f2({"foo","bar"}); } // chooses #4
427 namespace with_error
{
429 void f0(std::initializer_list
<int>); // #00 expected-note {{candidate function}}
430 void f0(std::initializer_list
<int>, int = 0); // expected-note {{candidate function}}
431 void g0() { f0({1L}); } // expected-error{{call to 'f0' is ambiguous}}
434 void f1(std::initializer_list
<long>); // #2 expected-note {{candidate function}}
435 void f1(std::initializer_list
<long>, int = 0); // expected-note {{candidate function}}
436 void g1() { f1({42}); } // expected-error{{call to 'f1' is ambiguous}}
438 void f2(std::pair
<const char*, const char*>); // #3
439 void f2(std::initializer_list
<std::string
>); // #4 expected-note {{candidate function}}
440 void f2(std::initializer_list
<std::string
>, int = 0); // expected-note {{candidate function}}
441 void g2() { f2({"foo","bar"}); } // expected-error{{call to 'f2' is ambiguous}}
446 namespace dr1591
{ //dr1591. Deducing array bound and element type from initializer list
447 template<class T
, int N
> int h(T
const(&)[N
]);
448 int X
= h({1,2,3}); // T deduced to int, N deduced to 3
450 template<class T
> int j(T
const(&)[3]);
451 int Y
= j({42}); // T deduced to int, array bound not considered
453 struct Aggr
{ int i
; int j
; };
454 template<int N
> int k(Aggr
const(&)[N
]); //expected-note{{not viable}}
455 int Y0
= k({1,2,3}); //expected-error{{no matching function}}
456 int Z
= k({{1},{2},{3}}); // OK, N deduced to 3
458 template<int M
, int N
> int m(int const(&)[M
][N
]);
459 int X0
= m({{1,2},{3,4}}); // M and N both deduced to 2
461 template<class T
, int N
> int n(T
const(&)[N
], T
);
462 int X1
= n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
465 namespace check_multi_dim_arrays
{
466 template<class T
, int N
, int M
, int O
> int ***f(const T (&a
)[N
][M
][O
]); //expected-note{{deduced conflicting values}}
467 template<class T
, int N
, int M
> int **f(const T (&a
)[N
][M
]); //expected-note{{couldn't infer}}
469 template<class T
, int N
> int *f(const T (&a
)[N
]); //expected-note{{couldn't infer}}
470 int ***p3
= f({ { {1,2}, {3, 4} }, { {5,6}, {7, 8} }, { {9,10}, {11, 12} } });
471 int ***p33
= f({ { {1,2}, {3, 4} }, { {5,6}, {7, 8} }, { {9,10}, {11, 12, 13} } }); //expected-error{{no matching}}
472 int **p2
= f({ {1,2,3}, {3, 4, 5} });
473 int **p22
= f({ {1,2}, {3, 4} });
474 int *p1
= f({1, 2, 3});
476 namespace check_multi_dim_arrays_rref
{
477 template<class T
, int N
, int M
, int O
> int ***f(T (&&a
)[N
][M
][O
]); //expected-note{{deduced conflicting values}}
478 template<class T
, int N
, int M
> int **f(T (&&a
)[N
][M
]); //expected-note{{couldn't infer}}
480 template<class T
, int N
> int *f(T (&&a
)[N
]); //expected-note{{couldn't infer}}
481 int ***p3
= f({ { {1,2}, {3, 4} }, { {5,6}, {7, 8} }, { {9,10}, {11, 12} } });
482 int ***p33
= f({ { {1,2}, {3, 4} }, { {5,6}, {7, 8} }, { {9,10}, {11, 12, 13} } }); //expected-error{{no matching}}
483 int **p2
= f({ {1,2,3}, {3, 4, 5} });
484 int **p22
= f({ {1,2}, {3, 4} });
485 int *p1
= f({1, 2, 3});
488 namespace check_arrays_of_init_list
{
489 template<class T
, int N
> float *f(const std::initializer_list
<T
> (&)[N
]);
490 template<class T
, int N
> double *f(const T(&)[N
]);
491 double *p
= f({1, 2, 3});
492 float *fp
= f({{1}, {1, 2}, {1, 2, 3}});
494 namespace core_reflector_28543
{
496 template<class T
, int N
> int *f(T (&&)[N
]); // #1
497 template<class T
> char *f(std::initializer_list
<T
> &&); //#2
498 template<class T
, int N
, int M
> int **f(T (&&)[N
][M
]); //#3 expected-note{{candidate}}
499 template<class T
, int N
> char **f(std::initializer_list
<T
> (&&)[N
]); //#4 expected-note{{candidate}}
501 template<class T
> short *f(T (&&)[2]); //#5
503 template<class T
> using Arr
= T
[];
505 char *pc
= f({1, 2, 3}); // OK prefer #2 via 13.3.3.2 [over.ics.rank]
506 char *pc2
= f({1, 2}); // #2 also
507 int *pi
= f(Arr
<int>{1, 2, 3}); // OK prefer #1
509 void *pv1
= f({ {1, 2, 3}, {4, 5, 6} }); // expected-error{{ambiguous}} btw 3 & 4
510 char **pcc
= f({ {1}, {2, 3} }); // OK #4
512 short *ps
= f(Arr
<int>{1, 2}); // OK #5