// RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors // RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors // RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors // RUN: %clang_cc1 -std=c++17 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors __extension__ typedef __SIZE_TYPE__ size_t; namespace std { template struct initializer_list { const T *ptr; size_t n; initializer_list(const T*, size_t); }; } namespace dr1310 { // dr1310: 5 struct S {} * sp = new S::S; // expected-error {{qualified reference to 'S' is a constructor name}} void f() { S::S(a); // expected-error {{qualified reference to 'S' is a constructor name}} } struct T { int n; typedef int U; typedef T V; }; int k = T().T::T::n; T::V v; struct U { int U; }; int u = U().U::U; struct U::U w; struct V : T::T { // FIXME: This is technically ill-formed, but we consider that to be a defect. V() : T::T() {} }; template struct VT : T::T { VT() : T::T() {} }; template struct VT; template class> class TT {}; template class TTy {}; template struct WBase {}; template struct W : WBase { typedef int X; int n; }; void w_test() { W::W w1a; // expected-error {{qualified reference to 'W' is a constructor name}} W::W::X w1ax; W::W w1b; // expected-error {{qualified reference to 'W' is a constructor name}} W::W::X w1bx; typename W::W w2a; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}} typename W::W::X w2ax; // expected-error 0-1{{outside of a template}} typename W::W w2b; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}} typename W::W::X w2bx; // expected-error 0-1{{outside of a template}} W::template W w3; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}} W::template W::X w3x; // expected-error 0-1{{outside of a template}} typename W::template W w4; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-2{{outside of a template}} typename W::template W::X w4x; // expected-error 0-2{{outside of a template}} TT::W> tt1; // expected-error {{qualified reference to 'W' is a constructor name}} TTy::W> tt1a; // expected-error {{qualified reference to 'W' is a constructor name}} TT::template W> tt2; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}} TT::WBase> tt3; TTy::WBase> tt3a; TT::template WBase> tt4; // expected-error 0-1{{outside of a template}} W w; (void)w.W::W::n; (void)w.W::W::n; (void)w.W::W::n; (void)w.W::template W::n; // expected-error 0-1{{outside of a template}} } template void wt_test() { typename W::W w2a; // expected-error {{qualified reference to 'W' is a constructor name}} typename W::template W w4; // expected-error {{qualified reference to 'W' is a constructor name}} TTy tt2; // expected-error {{qualified reference to 'W' is a constructor name}} TT tt3; // expected-error {{qualified reference to 'W' is a constructor name}} } template void wt_test_good() { typename W::W::X w2ax; typename W::template W::X w4x; TTy tt4; TT tt5; W w; (void)w.W::W::n; (void)w.W::template W::n; (void)w.template W::W::n; (void)w.template W::template W::n; } template void wt_test >(); // expected-note {{instantiation of}} template void wt_test_good >(); } namespace dr1315 { // dr1315: partial template struct A {}; template // expected-note {{non-deducible template parameter 'I'}} struct A {}; // expected-error {{contains a template parameter that cannot be deduced}} template struct A {}; template struct B; template struct B {}; // expected-note {{matches}} B<1, 2, 3> b1; // Multiple declarations with the same dependent expression are equivalent // for partial ordering purposes. template struct B { typedef int type; }; B<1, 2, 2>::type b2; // Multiple declarations with differing dependent expressions are unordered. template struct B {}; // expected-note {{matches}} B<1, 2, 4> b3; // expected-error {{ambiguous}} // FIXME: Under dr1315, this is perhaps valid, but that is not clear: this // fails the "more specialized than the primary template" test because the // dependent type of T::value is not the same as 'int'. // A core issue will be opened to decide what is supposed to happen here. template struct C; template struct C; // expected-error@-1 {{type of specialized non-type template argument depends on a template parameter of the partial specialization}} } namespace dr1330 { // dr1330: 4 c++11 // exception-specifications are parsed in a context where the class is complete. struct A { void f() throw(T) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}} struct T {}; #if __cplusplus >= 201103L void g() noexcept(&a == b) {} static int a; static constexpr int *b = &a; #endif }; void (A::*af1)() throw(A::T) = &A::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}} void (A::*af2)() throw() = &A::f; // expected-error-re {{{{not superset|different exception spec}}}} #if __cplusplus >= 201103L static_assert(noexcept(A().g()), ""); #endif // Likewise, they're instantiated separately from an enclosing class template. template struct B { void f() throw(T, typename U::type) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}} struct T {}; #if __cplusplus >= 201103L void g() noexcept(&a == b && U::value) {} static int a; static constexpr int *b = &a; #endif }; B bi; // ok struct P { typedef int type; static const int value = true; }; void (B

::*bpf1)() throw(B

::T, int) = &B

::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}} #if __cplusplus < 201103L // expected-error@-2 {{not superset}} // FIXME: We only delay instantiation in C++11 onwards. In C++98, something // weird happens: instantiation of B

fails because it references T before // it's instantiated, but the diagnostic is suppressed in // Sema::FindInstantiatedDecl because we've already hit an error. This is // obviously a bad way to react to this situation; we should still producing // the "T has not yet been instantiated" error here, rather than giving // confusing errors later on. #endif void (B

::*bpf2)() throw(int) = &B

::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}} #if __cplusplus <= 201402L // expected-error@-2 {{not superset}} #else // expected-warning@-4 {{not superset}} #endif void (B

::*bpf3)() = &B

::f; void (B

::*bpf4)() throw() = &B

::f; #if __cplusplus <= 201402L // expected-error@-2 {{not superset}} #else // expected-error@-4 {{different exception specifications}} #endif #if __cplusplus >= 201103L static_assert(noexcept(B

().g()), ""); struct Q { static const int value = false; }; static_assert(!noexcept(B().g()), ""); #endif template 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}} #if __cplusplus > 201402L // expected-error@-2 0-1{{C++17}} expected-note@-2 0-1{{noexcept}} #endif // An exception-specification is needed even if the function is only used in // an unevaluated operand. int f1 = sizeof(f()); // expected-note {{instantiation of}} #if __cplusplus >= 201103L decltype(f()) f2; // expected-note {{instantiation of}} bool f3 = noexcept(f()); // expected-note {{instantiation of}} #endif // In C++17 onwards, substituting explicit template arguments into the // function type substitutes into the exception specification (because it's // part of the type). In earlier languages, we don't notice there's a problem // until we've already started to instantiate. template int f(); #if __cplusplus >= 201703L // expected-error@-2 {{does not refer to a function template}} #else // expected-note@-4 {{instantiation of}} #endif template struct C { C() throw(typename T::type); // expected-error 1-2{{prior to '::'}} #if __cplusplus > 201402L // expected-error@-2 0-1{{C++17}} expected-note@-2 0-1{{noexcept}} #endif }; struct D : C {}; // ok #if __cplusplus < 201103L // expected-note@-2 {{instantiation of}} #endif void f(D &d) { d = d; } // ok struct E : C {}; // expected-note {{in instantiation of}} #if __cplusplus >= 201103L E e; // expected-note {{needed here}} #endif } namespace dr1346 { // dr1346: 3.5 auto a(1); // expected-error 0-1{{extension}} auto b(1, 2); // expected-error {{multiple expressions}} expected-error 0-1{{extension}} #if __cplusplus >= 201103L auto c({}); // expected-error {{parenthesized initializer list}} auto d({1}); // expected-error {{parenthesized initializer list}} auto e({1, 2}); // expected-error {{parenthesized initializer list}} #endif template void f(Ts ...ts) { // expected-error 0-1{{extension}} auto x(ts...); // expected-error {{empty}} expected-error 0-1{{extension}} } template void f(); // expected-note {{instantiation}} #if __cplusplus >= 201103L void init_capture() { [a(1)] {} (); // expected-error 0-1{{extension}} [b(1, 2)] {} (); // expected-error {{multiple expressions}} expected-error 0-1{{extension}} #if __cplusplus >= 201103L [c({})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}} [d({1})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}} [e({1, 2})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}} #endif } #endif } namespace dr1347 { // dr1347: yes auto x = 5, *y = &x; // expected-error 0-1{{extension}} 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}} #if __cplusplus >= 201103L auto a = 5, b = {1, 2}; // expected-error {{'auto' deduced as 'int' in declaration of 'a' and deduced as 'std::initializer_list' in declaration of 'b'}} auto (*fp)(int) -> int, i = 0; // expected-error {{declaration with trailing return type must be the only declaration in its group}} #endif } namespace dr1358 { // dr1358: yes #if __cplusplus >= 201103L struct Lit { constexpr operator int() const { return 0; } }; struct NonLit { NonLit(); operator int(); }; // expected-note 2{{no constexpr constructors}} struct NonConstexprConv { constexpr operator int() const; }; struct Virt { virtual int f(int) const; }; template struct A : V { int member; constexpr A(U u) : member(u) {} constexpr T f(U u) const { return T(); } }; constexpr A ce = Lit(); constexpr int k = ce.f(Lit{}); // Can have a non-literal return type and parameter type. // Constexpr function can be implicitly virtual. A a = NonLit(); void g() { a.f(NonLit()); } // Constructor is still constexpr, so this is a literal type. static_assert(__is_literal_type(decltype(a)), ""); // Constructor can call non-constexpr functions. A b = NonConstexprConv(); // But the corresponding non-template cases are rejected. struct B : Virt { int member; constexpr B(NonLit u) : member(u) {} // expected-error {{not a literal type}} constexpr NonLit f(NonLit u) const { return NonLit(); } // expected-error {{not a literal type}} }; #endif } namespace dr1359 { // dr1359: 3.5 #if __cplusplus >= 201103L union A { constexpr A() = default; }; union B { constexpr B() = default; int a; }; // expected-error {{not constexpr}} expected-note 2{{candidate}} union C { constexpr C() = default; int a, b; }; // expected-error {{not constexpr}} expected-note 2{{candidate}} struct X { constexpr X() = default; union {}; }; // expected-error {{does not declare anything}} struct Y { constexpr Y() = default; union { int a; }; }; // expected-error {{not constexpr}} expected-note 2{{candidate}} constexpr A a = A(); constexpr B b = B(); // expected-error {{no matching}} constexpr C c = C(); // expected-error {{no matching}} constexpr X x = X(); constexpr Y y = Y(); // expected-error {{no matching}} #endif } namespace dr1388 { // dr1388: 4 template void f(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}} template void g(T..., int); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}} template void h(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}} void test_f() { f(0); // ok, trailing parameter pack deduced to empty f(0, 0); // expected-error {{no matching}} f(0); f(0, 0); // expected-error {{no matching}} f(0, 0); f(0, 0); // expected-error {{no matching}} g(0); g(0, 0); // expected-error {{no matching}} g<>(0); g(0); // expected-error {{no matching}} g(0, 0); h(0); h(0, 0); // expected-error {{no matching}} h(0, 0); h(0, 0); // expected-error {{no matching}} } // A non-trailing parameter pack is still a non-deduced context, even though // we know exactly how many arguments correspond to it. template struct pair {}; template struct tuple { typedef char type; }; // expected-error 0-2{{C++11}} template void f_pair_1(pair..., int); // expected-error 0-2{{C++11}} expected-note {{[with T = ]: deduced incomplete pack <(no value), (no value)> for template parameter 'U'}} template void f_pair_2(pair..., U); // expected-error 0-2{{C++11}} template void f_pair_3(pair..., tuple); // expected-error 0-2{{C++11}} expected-note {{deduced packs of different lengths for parameter 'U' (<(no value), (no value)> vs. )}} template void f_pair_4(pair..., T...); // expected-error 0-2{{C++11}} expected-note {{ vs. }} void g(pair a, pair b, tuple c) { f_pair_1(a, b, 0); // expected-error {{no match}} f_pair_2(a, b, 0); f_pair_3(a, b, c); f_pair_3(a, b, tuple()); // expected-error {{no match}} f_pair_4(a, b, 0, 0L); f_pair_4(a, b, 0, 0L, "foo"); // expected-error {{no match}} } } namespace dr1391 { // dr1391: partial struct A {}; struct B : A {}; template struct C { C(int); typename T::error error; }; // expected-error 2{{'::'}} template struct D {}; // No deduction is performed for parameters with no deducible template-parameters, therefore types do not need to match. template void a(T, int T::*); void test_a(int A::*p) { a(A(), p); } // ok, type of second parameter does not need to match namespace dr_example_1 { template void f(C); template void f(D); void g(D d) { f(d); // ok, first 'f' eliminated by deduction failure f(d); // ok, first 'f' eliminated because 'U' cannot be deduced } } namespace dr_example_2 { template typename C::error f(int, T); template T f(T, T); void g(A a) { f(a, a); // ok, no conversion from A to int for first parameter of first candidate } } namespace std_example { template struct Z { typedef typename T::x xx; }; template typename Z::xx f(void *, T); template void f(int, T); struct A {} a; void g() { f(1, a); } } template void b(C ci, T *p); void b(...); void test_b() { b(0, 0); // ok, deduction fails prior to forming a conversion sequence and instantiating C // FIXME: The "while substituting" note should point at the overload candidate. b(0, 0); // expected-note {{instantiation of}} expected-note {{while substituting}} } template struct Id { typedef T type; }; template void c(T, typename Id >::type); void test_c() { // Implicit conversion sequences for dependent types are checked later. c(0.0, 0); // expected-note {{instantiation of}} } namespace partial_ordering { // FIXME: Second template should be considered more specialized because non-dependent parameter is ignored. template int a(T, short) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}} template int a(T*, char); // expected-note {{candidate}} int test_a = a((int*)0, 0); // FIXME: expected-error {{ambiguous}} // FIXME: Second template should be considered more specialized: // deducing #1 from #2 ignores the second P/A pair, so deduction succeeds, // deducing #2 from #1 fails to deduce T, so deduction fails. template int b(T, int) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}} template int b(T*, U); // expected-note {{candidate}} int test_b = b((int*)0, 0); // FIXME: expected-error {{ambiguous}} // Unintended consequences: because partial ordering does not consider // explicit template arguments, and deduction from a non-dependent type // vacuously succeeds, a non-dependent template is less specialized than // anything else! // According to DR1391, this is ambiguous! template int c(int); template int c(T); int test_c1 = c(0); // ok int test_c2 = c(0); // FIXME: apparently ambiguous } } namespace dr1399 { // dr1399: dup 1388 template void f(T..., int, T...) {} // expected-note {{candidate}} expected-error 0-1{{C++11}} void g() { f(0); f(0, 0, 0); f(0, 0, 0); // expected-error {{no match}} } }