Clang Project

clang_source_code/test/CXX/drs/dr13xx.cpp
1// RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2// RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3// RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4// RUN: %clang_cc1 -std=c++17 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
5
6__extension__ typedef __SIZE_TYPE__ size_t;
7
8namespace std {
9  template<typename T> struct initializer_list {
10    const T *ptr;
11    size_t n;
12    initializer_list(const T*, size_t);
13  };
14}
15
16namespace dr1310 { // dr1310: 5
17  struct S {} * sp = new S::S; // expected-error {{qualified reference to 'S' is a constructor name}}
18  void f() {
19    S::S(a); // expected-error {{qualified reference to 'S' is a constructor name}}
20  }
21  struct T { int n; typedef int U; typedef T V; };
22  int k = T().T::T::n;
23  T::V v;
24
25  struct U { int U; };
26  int u = U().U::U;
27  struct U::U w;
28
29  struct V : T::T {
30    // FIXME: This is technically ill-formed, but we consider that to be a defect.
31    V() : T::T() {}
32  };
33  template<typename T> struct VT : T::T {
34    VT() : T::T() {}
35  };
36  template struct VT<T>;
37
38  template<template<typename> class> class TT {};
39  template<typename> class TTy {};
40
41  template<typename T> struct WBase {};
42  template<typename T> struct W : WBase<T> { typedef int X; int n; };
43
44  void w_test() {
45    W<int>::W w1a; // expected-error {{qualified reference to 'W' is a constructor name}}
46    W<int>::W::X w1ax;
47    W<int>::W<int> w1b; // expected-error {{qualified reference to 'W' is a constructor name}}
48    W<int>::W<int>::X w1bx;
49    typename W<int>::W w2a; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
50    typename W<int>::W::X w2ax; // expected-error 0-1{{outside of a template}}
51    typename W<int>::W<int> w2b; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
52    typename W<int>::W<int>::X w2bx; // expected-error 0-1{{outside of a template}}
53    W<int>::template W<int> w3; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
54    W<int>::template W<int>::X w3x; // expected-error 0-1{{outside of a template}}
55    typename W<int>::template W<int> w4; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-2{{outside of a template}}
56    typename W<int>::template W<int>::X w4x; // expected-error 0-2{{outside of a template}}
57
58    TT<W<int>::W> tt1; // expected-error {{qualified reference to 'W' is a constructor name}}
59    TTy<W<int>::W> tt1a; // expected-error {{qualified reference to 'W' is a constructor name}}
60    TT<W<int>::template W> tt2; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
61    TT<W<int>::WBase> tt3;
62    TTy<W<int>::WBase> tt3a;
63    TT<W<int>::template WBase> tt4; // expected-error 0-1{{outside of a template}}
64
65    W<int> w;
66    (void)w.W::W::n;
67    (void)w.W<int>::W::n;
68    (void)w.W<int>::W<int>::n;
69    (void)w.W<int>::template W<int>::n; // expected-error 0-1{{outside of a template}}
70  }
71
72  template<typename W>
73  void wt_test() {
74    typename W::W w2a; // expected-error {{qualified reference to 'W' is a constructor name}}
75    typename W::template W<int> w4; // expected-error {{qualified reference to 'W' is a constructor name}}
76    TTy<typename W::W> tt2; // expected-error {{qualified reference to 'W' is a constructor name}}
77    TT<W::template W> tt3; // expected-error {{qualified reference to 'W' is a constructor name}}
78  }
79  template<typename W>
80  void wt_test_good() {
81    typename W::W::X w2ax;
82    typename W::template W<int>::X w4x;
83    TTy<typename W::WBase> tt4;
84    TT<W::template WBase> tt5;
85
86    W w;
87    (void)w.W::W::n;
88    (void)w.W::template W<int>::n;
89    (void)w.template W<int>::W::n;
90    (void)w.template W<int>::template W<int>::n;
91  }
92  template void wt_test<W<int> >(); // expected-note {{instantiation of}}
93  template void wt_test_good<W<int> >();
94}
95
96namespace dr1315 { // dr1315: partial
97  template <int I, int J> struct A {};
98  template <int I> // expected-note {{non-deducible template parameter 'I'}}
99    struct A<I + 5, I * 2> {}; // expected-error {{contains a template parameter that cannot be deduced}}
100  template <int I> struct A<I, I> {};
101
102  template <int I, int J, int K> struct B;
103  template <int I, int K> struct B<I, I * 2, K> {}; // expected-note {{matches}}
104  B<1, 2, 3> b1;
105
106  // Multiple declarations with the same dependent expression are equivalent
107  // for partial ordering purposes.
108  template <int I> struct B<I, I * 2, 2> { typedef int type; };
109  B<1, 2, 2>::type b2;
110
111  // Multiple declarations with differing dependent expressions are unordered.
112  template <int I, int K> struct B<I, I + 1, K> {}; // expected-note {{matches}}
113  B<1, 2, 4> b3; // expected-error {{ambiguous}}
114
115  // FIXME: Under dr1315, this is perhaps valid, but that is not clear: this
116  // fails the "more specialized than the primary template" test because the
117  // dependent type of T::value is not the same as 'int'.
118  // A core issue will be opened to decide what is supposed to happen here.
119  template <typename T, int I> struct C;
120  template <typename T> struct C<T, T::value>;
121  // expected-error@-1 {{type of specialized non-type template argument depends on a template parameter of the partial specialization}}
122}
123
124namespace dr1330 { // dr1330: 4 c++11
125  // exception-specifications are parsed in a context where the class is complete.
126  struct A {
127    void f() throw(T) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
128    struct T {};
129
130#if __cplusplus >= 201103L
131    void g() noexcept(&a == b) {}
132    static int a;
133    static constexpr int *b = &a;
134#endif
135  };
136
137  void (A::*af1)() throw(A::T) = &A::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
138  void (A::*af2)() throw() = &A::f; // expected-error-re {{{{not superset|different exception spec}}}}
139
140#if __cplusplus >= 201103L
141  static_assert(noexcept(A().g()), "");
142#endif
143
144  // Likewise, they're instantiated separately from an enclosing class template.
145  template<typename U>
146  struct B {
147    void f() throw(T, typename U::type) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
148    struct T {};
149
150#if __cplusplus >= 201103L
151    void g() noexcept(&a == b && U::value) {}
152    static int a;
153    static constexpr int *b = &a;
154#endif
155  };
156
157  B<int> bi; // ok
158
159  struct P {
160    typedef int type;
161    static const int value = true;
162  };
163
164  void (B<P>::*bpf1)() throw(B<P>::T, int) = &B<P>::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
165#if __cplusplus < 201103L
166  // expected-error@-2 {{not superset}}
167  // FIXME: We only delay instantiation in C++11 onwards. In C++98, something
168  // weird happens: instantiation of B<P> fails because it references T before
169  // it's instantiated, but the diagnostic is suppressed in
170  // Sema::FindInstantiatedDecl because we've already hit an error. This is
171  // obviously a bad way to react to this situation; we should still producing
172  // the "T has not yet been instantiated" error here, rather than giving
173  // confusing errors later on.
174#endif
175  void (B<P>::*bpf2)() throw(int) = &B<P>::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
176#if __cplusplus <= 201402L
177  // expected-error@-2 {{not superset}}
178#else
179  // expected-warning@-4 {{not superset}}
180#endif
181  void (B<P>::*bpf3)() = &B<P>::f;
182  void (B<P>::*bpf4)() throw() = &B<P>::f;
183#if __cplusplus <= 201402L
184  // expected-error@-2 {{not superset}}
185#else
186  // expected-error@-4 {{different exception specifications}}
187#endif
188
189#if __cplusplus >= 201103L
190  static_assert(noexcept(B<P>().g()), "");
191  struct Q { static const int value = false; };
192  static_assert(!noexcept(B<Q>().g()), "");
193#endif
194
195  template<typename T> int f() throw(typename T::error) { return 0; } // expected-error 1-4{{prior to '::'}} expected-note 0-1{{prior to '::'}} expected-note 0-1{{requested here}}
196#if __cplusplus > 201402L
197    // expected-error@-2 0-1{{C++17}} expected-note@-2 0-1{{noexcept}}
198#endif
199  // An exception-specification is needed even if the function is only used in
200  // an unevaluated operand.
201  int f1 = sizeof(f<int>()); // expected-note {{instantiation of}}
202#if __cplusplus >= 201103L
203  decltype(f<char>()) f2; // expected-note {{instantiation of}}
204  bool f3 = noexcept(f<float>()); // expected-note {{instantiation of}}
205#endif
206  // In C++17 onwards, substituting explicit template arguments into the
207  // function type substitutes into the exception specification (because it's
208  // part of the type). In earlier languages, we don't notice there's a problem
209  // until we've already started to instantiate.
210  template int f<short>();
211#if __cplusplus >= 201703L
212  // expected-error@-2 {{does not refer to a function template}}
213#else
214  // expected-note@-4 {{instantiation of}}
215#endif
216
217  template<typename T> struct C {
218    C() throw(typename T::type); // expected-error 1-2{{prior to '::'}}
219#if __cplusplus > 201402L
220    // expected-error@-2 0-1{{C++17}} expected-note@-2 0-1{{noexcept}}
221#endif
222  };
223  struct D : C<void> {}; // ok
224#if __cplusplus < 201103L
225  // expected-note@-2 {{instantiation of}}
226#endif
227  void f(D &d) { d = d; } // ok
228
229  struct E : C<int> {}; // expected-note {{in instantiation of}}
230#if __cplusplus >= 201103L
231  E e; // expected-note {{needed here}}
232#endif
233}
234
235namespace dr1346 { // dr1346: 3.5
236  auto a(1); // expected-error 0-1{{extension}}
237  auto b(1, 2); // expected-error {{multiple expressions}} expected-error 0-1{{extension}}
238#if __cplusplus >= 201103L
239  auto c({}); // expected-error {{parenthesized initializer list}}
240  auto d({1}); // expected-error {{parenthesized initializer list}}
241  auto e({1, 2}); // expected-error {{parenthesized initializer list}}
242#endif
243  template<typename...Ts> void f(Ts ...ts) { // expected-error 0-1{{extension}}
244    auto x(ts...); // expected-error {{empty}} expected-error 0-1{{extension}}
245  }
246  template void f(); // expected-note {{instantiation}}
247
248#if __cplusplus >= 201103L
249  void init_capture() {
250    [a(1)] {} (); // expected-error 0-1{{extension}}
251    [b(1, 2)] {} (); // expected-error {{multiple expressions}} expected-error 0-1{{extension}}
252#if __cplusplus >= 201103L
253    [c({})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}}
254    [d({1})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}}
255    [e({1, 2})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}}
256#endif
257  }
258#endif
259}
260
261namespace dr1347 { // dr1347: yes
262  auto x = 5, *y = &x; // expected-error 0-1{{extension}}
263  auto z = y, *q = y; // expected-error {{'auto' deduced as 'int *' in declaration of 'z' and deduced as 'int' in declaration of 'q'}} expected-error 0-1{{extension}}
264#if __cplusplus >= 201103L
265  auto a = 5, b = {1, 2}; // expected-error {{'auto' deduced as 'int' in declaration of 'a' and deduced as 'std::initializer_list<int>' in declaration of 'b'}}
266  auto (*fp)(int) -> int, i = 0; // expected-error {{declaration with trailing return type must be the only declaration in its group}}
267#endif
268}
269
270namespace dr1359 { // dr1359: 3.5
271#if __cplusplus >= 201103L
272  union A { constexpr A() = default; };
273  union B { constexpr B() = default; int a; }; // expected-error {{not constexpr}} expected-note 2{{candidate}}
274  union C { constexpr C() = default; int a, b; }; // expected-error {{not constexpr}} expected-note 2{{candidate}}
275  struct X { constexpr X() = default; union {}; };
276  struct Y { constexpr Y() = default; union { int a; }; }; // expected-error {{not constexpr}} expected-note 2{{candidate}}
277
278  constexpr A a = A();
279  constexpr B b = B(); // expected-error {{no matching}}
280  constexpr C c = C(); // expected-error {{no matching}}
281  constexpr X x = X();
282  constexpr Y y = Y(); // expected-error {{no matching}}
283#endif
284}
285
286namespace dr1388 { // dr1388: 4
287  template<typename A, typename ...T> void f(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}}
288  template<typename ...T> void g(T..., int); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}}
289  template<typename ...T, typename A> void h(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}}
290
291  void test_f() { 
292    f(0); // ok, trailing parameter pack deduced to empty
293    f(0, 0); // expected-error {{no matching}}
294    f<int>(0);
295    f<int>(0, 0); // expected-error {{no matching}}
296    f<int, int>(0, 0);
297    f<int, int, int>(0, 0); // expected-error {{no matching}}
298
299    g(0);
300    g(0, 0); // expected-error {{no matching}}
301    g<>(0);
302    g<int>(0); // expected-error {{no matching}}
303    g<int>(0, 0);
304
305    h(0);
306    h(0, 0); // expected-error {{no matching}}
307    h<int>(0, 0);
308    h<int, int>(0, 0); // expected-error {{no matching}}
309  }
310
311  // A non-trailing parameter pack is still a non-deduced context, even though
312  // we know exactly how many arguments correspond to it.
313  template<typename T, typename U> struct pair {};
314  template<typename ...T> struct tuple { typedef char type; }; // expected-error 0-2{{C++11}}
315  template<typename ...T, typename ...U> void f_pair_1(pair<T, U>..., int); // expected-error 0-2{{C++11}} expected-note {{different lengths (2 vs. 0)}}
316  template<typename ...T, typename U> void f_pair_2(pair<T, char>..., U); // expected-error 0-2{{C++11}}
317  template<typename ...T, typename ...U> void f_pair_3(pair<T, U>..., tuple<U...>); // expected-error 0-2{{C++11}} expected-note {{different lengths (2 vs. 1)}}
318  template<typename ...T> void f_pair_4(pair<T, char>..., T...); // expected-error 0-2{{C++11}} expected-note {{<int, long> vs. <int, long, const char *>}}
319  void g(pair<int, char> a, pair<long, char> b, tuple<char, char> c) {
320    f_pair_1<int, long>(a, b, 0); // expected-error {{no match}}
321    f_pair_2<int, long>(a, b, 0);
322    f_pair_3<int, long>(a, b, c);
323    f_pair_3<int, long>(a, b, tuple<char>()); // expected-error {{no match}}
324    f_pair_4<int, long>(a, b, 0, 0L);
325    f_pair_4<int, long>(a, b, 0, 0L, "foo"); // expected-error {{no match}}
326  }
327}
328
329namespace dr1391 { // dr1391: partial
330  struct A {}; struct B : A {};
331  template<typename T> struct C { C(int); typename T::error error; }; // expected-error 2{{'::'}}
332  template<typename T> struct D {};
333
334  // No deduction is performed for parameters with no deducible template-parameters, therefore types do not need to match.
335  template<typename T> void a(T, int T::*);
336  void test_a(int A::*p) { a(A(), p); } // ok, type of second parameter does not need to match
337
338  namespace dr_example_1 {
339    template<typename T, typename U> void f(C<T>);
340    template<typename T> void f(D<T>);
341
342    void g(D<int> d) {
343      f(d); // ok, first 'f' eliminated by deduction failure
344      f<int>(d); // ok, first 'f' eliminated because 'U' cannot be deduced
345    }
346  }
347
348  namespace dr_example_2 {
349    template<typename T> typename C<T>::error f(int, T);
350    template<typename T> T f(T, T);
351
352    void g(A a) {
353      f(a, a); // ok, no conversion from A to int for first parameter of first candidate
354    }
355  }
356
357  namespace std_example {
358    template<typename T> struct Z {
359      typedef typename T::x xx;
360    };
361    template<typename T> typename Z<T>::xx f(void *, T);
362    template<typename T> void f(int, T);
363    struct A {} a;
364    void g() { f(1, a); }
365  }
366
367  template<typename T> void b(C<int> ci, T *p);
368  void b(...);
369  void test_b() {
370    b(0, 0); // ok, deduction fails prior to forming a conversion sequence and instantiating C<int>
371    // FIXME: The "while substituting" note should point at the overload candidate.
372    b<int>(0, 0); // expected-note {{instantiation of}} expected-note {{while substituting}}
373  }
374
375  template<typename T> struct Id { typedef T type; };
376  template<typename T> void c(T, typename Id<C<T> >::type);
377  void test_c() {
378    // Implicit conversion sequences for dependent types are checked later.
379    c(0.0, 0); // expected-note {{instantiation of}}
380  }
381
382  namespace partial_ordering {
383    // FIXME: Second template should be considered more specialized because non-dependent parameter is ignored.
384    template<typename T> int a(T, short) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}}
385    template<typename T> int a(T*, char); // expected-note {{candidate}}
386    int test_a = a((int*)0, 0); // FIXME: expected-error {{ambiguous}}
387
388    // FIXME: Second template should be considered more specialized:
389    // deducing #1 from #2 ignores the second P/A pair, so deduction succeeds,
390    // deducing #2 from #1 fails to deduce T, so deduction fails.
391    template<typename T> int b(T, int) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}}
392    template<typename T, typename U> int b(T*, U); // expected-note {{candidate}}
393    int test_b = b((int*)0, 0); // FIXME: expected-error {{ambiguous}}
394
395    // Unintended consequences: because partial ordering does not consider
396    // explicit template arguments, and deduction from a non-dependent type
397    // vacuously succeeds, a non-dependent template is less specialized than
398    // anything else!
399    // According to DR1391, this is ambiguous!
400    template<typename T> int c(int);
401    template<typename T> int c(T);
402    int test_c1 = c(0); // ok
403    int test_c2 = c<int>(0); // FIXME: apparently ambiguous
404  }
405}
406
407namespace dr1399 { // dr1399: dup 1388
408  template<typename ...T> void f(T..., int, T...) {} // expected-note {{candidate}} expected-error 0-1{{C++11}}
409  void g() {
410    f(0);
411    f<int>(0, 0, 0);
412    f(0, 0, 0); // expected-error {{no match}}
413  }
414}
415