Home | History | Annotate | Download | only in SemaTemplate
      1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -ftemplate-depth 16 -fcxx-exceptions -fexceptions %s
      2 
      3 // DR1330: an exception specification for a function template is only
      4 // instantiated when it is needed.
      5 
      6 template<typename T> void f1(T*) throw(T); // expected-error{{incomplete type 'Incomplete' is not allowed in exception specification}}
      7 struct Incomplete; // expected-note{{forward}}
      8 
      9 void test_f1(Incomplete *incomplete_p, int *int_p) {
     10   f1(int_p);
     11   f1(incomplete_p); // expected-note{{instantiation of exception spec}}
     12 }
     13 
     14 template<typename T> struct A {
     15   template<typename U> struct B {
     16     static void f() noexcept(A<U>().n);
     17   };
     18 
     19   constexpr A() : n(true) {}
     20   bool n;
     21 };
     22 
     23 static_assert(noexcept(A<int>::B<char>::f()), "");
     24 
     25 template<unsigned N> struct S {
     26   static void recurse() noexcept(noexcept(S<N+1>::recurse())); // \
     27   // expected-error {{no member named 'recurse'}} \
     28   // expected-note 9{{instantiation of exception spec}}
     29 };
     30 decltype(S<0>::recurse()) *pVoid1 = 0; // ok, exception spec not needed
     31 decltype(&S<0>::recurse) pFn = 0; // ok, exception spec not needed
     32 
     33 template<> struct S<10> {};
     34 void (*pFn2)() noexcept = &S<0>::recurse; // expected-note {{instantiation of exception spec}} expected-error {{not superset}}
     35 
     36 
     37 namespace dr1330_example {
     38   template <class T> struct A {
     39     void f(...) throw (typename T::X); // expected-error {{'int'}}
     40     void f(int);
     41   };
     42 
     43   int main() {
     44     A<int>().f(42);
     45   }
     46 
     47   int test2() {
     48     struct S {
     49       template<typename T>
     50       static int f() noexcept(noexcept(A<T>().f("boo!"))) { return 0; } // \
     51       // expected-note {{instantiation of exception spec}}
     52       typedef decltype(f<S>()) X;
     53     };
     54     S().f<S>(); // ok
     55     S().f<int>(); // expected-note {{instantiation of exception spec}}
     56   }
     57 }
     58 
     59 namespace core_19754_example {
     60   template<typename T> T declval() noexcept;
     61 
     62   template<typename T, typename = decltype(T(declval<T&&>()))>
     63   struct is_movable { static const bool value = true; };
     64 
     65   template<typename T>
     66   struct wrap {
     67     T val;
     68     void irrelevant(wrap &p) noexcept(is_movable<T>::value);
     69   };
     70 
     71   template<typename T>
     72   struct base {
     73      base() {}
     74      base(const typename T::type1 &);
     75      base(const typename T::type2 &);
     76   };
     77 
     78   template<typename T>
     79   struct type1 {
     80      wrap<typename T::base> base;
     81   };
     82 
     83   template<typename T>
     84   struct type2 {
     85      wrap<typename T::base> base;
     86   };
     87 
     88   struct types {
     89      typedef base<types> base;
     90      typedef type1<types> type1;
     91      typedef type2<types> type2;
     92   };
     93 
     94   base<types> val = base<types>();
     95 }
     96 
     97 namespace pr9485 {
     98   template <typename T> void f1(T) throw(typename T::exception); // expected-note {{candidate}}
     99   template <typename T> void f1(T, int = 0) throw(typename T::noitpecxe); // expected-note {{candidate}}
    100 
    101   template <typename T> void f2(T) noexcept(T::throws); // expected-note {{candidate}}
    102   template <typename T> void f2(T, int = 0) noexcept(T::sworht); // expected-note {{candidate}}
    103 
    104   void test() {
    105     f1(0); // expected-error {{ambiguous}}
    106     f2(0); // expected-error {{ambiguous}}
    107   }
    108 }
    109 
    110 struct Exc1 { char c[4]; };
    111 struct Exc2 { double x, y, z; };
    112 struct Base {
    113   virtual void f() noexcept; // expected-note {{overridden}}
    114 };
    115 template<typename T> struct Derived : Base {
    116   void f() noexcept (sizeof(T) == 4); // expected-error {{is more lax}}
    117   void g() noexcept (T::error);
    118 };
    119 
    120 Derived<Exc1> d1; // ok
    121 Derived<Exc2> d2; // expected-note {{in instantiation of}}
    122 
    123 // If the vtable for a derived class is used, the exception specification of
    124 // any member function which ends up in that vtable is needed, even if it was
    125 // declared in a base class.
    126 namespace PR12763 {
    127   template<bool *B> struct T {
    128     virtual void f() noexcept (*B); // expected-error {{constant expression}} expected-note {{read of non-const}}
    129   };
    130   bool b; // expected-note {{here}}
    131   struct X : public T<&b> {
    132     virtual void g();
    133   };
    134   void X::g() {} // expected-note {{in instantiation of}}
    135 }
    136