brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.6 KiB · 4612e4a Raw
174 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat %s2// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -Wc++11-compat -std=c++98 %s3// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=c++11 %s4// RUN: %clang_cc1 -fsyntax-only -verify -pedantic -std=c++20 %s5//6// Tests explicit instantiation of templates.7template<typename T, typename U = T> class X0 { };8 9namespace N {10  template<typename T, typename U = T> class X1 { };11}12 13// Check the syntax of explicit instantiations.14template class X0<int, float>;15template class X0<int>; // expected-note{{previous}}16 17template class N::X1<int>;18template class ::N::X1<int, float>;19 20using namespace N;21 22// Check for some bogus syntax that probably means that the user23// wanted to write an explicit specialization, but forgot the '<>'24// after 'template'.25template class X0<double> { }; // expected-error{{explicit specialization}}26 27// Check for explicit instantiations that come after other kinds of28// instantiations or declarations.29template class X0<int, int>; // expected-error{{duplicate}}30 31template<> class X0<char> { }; // expected-note{{previous}}32template class X0<char>; // expected-warning{{has no effect}}33 34void foo(X0<short>) { }35template class X0<short>;36 37// Check that explicit instantiations actually produce definitions. We38// determine whether this happens by placing semantic errors in the39// definition of the template we're instantiating.40template<typename T> struct X2; // expected-note{{declared here}}41 42template struct X2<float>; // expected-error{{undefined template}}43 44template<typename T>45struct X2 {46  void f0(T*); // expected-error{{pointer to a reference}}47};48 49template struct X2<int>; // okay50template struct X2<int&>; // expected-note{{in instantiation of}}51 52// Check that explicit instantiations instantiate member classes.53template<typename T> struct X3 {54  struct Inner {55    void f(T*); // expected-error{{pointer to a reference}}56  };57};58 59void f1(X3<int&>); // okay, Inner, not instantiated60 61template struct X3<int&>; // expected-note{{instantiation}}62 63template<typename T> struct X4 {64  struct Inner {65    struct VeryInner {66      void f(T*); // expected-error 2{{pointer to a reference}}67    };68  };69};70 71void f2(X4<int&>); // okay, Inner, not instantiated72void f3(X4<int&>::Inner); // okay, Inner::VeryInner, not instantiated73 74template struct X4<int&>; // expected-note{{instantiation}}75template struct X4<float&>; // expected-note{{instantiation}}76 77// Check explicit instantiation of member classes78namespace N2 {79 80template<typename T>81struct X5 {82  struct Inner1 {83    void f(T&);84  };85 86  struct Inner2 { // expected-note {{here}}87    struct VeryInner {88      void g(T*); // expected-error 2{{pointer to a reference}}89    };90  };91};92 93}94 95template struct N2::X5<void>::Inner2;96 97using namespace N2;98template struct X5<int&>::Inner2; // expected-note{{instantiation}}99 100void f4(X5<float&>::Inner2);101template struct X5<float&>::Inner2; // expected-note{{instantiation}}102 103namespace N3 {104  template struct N2::X5<int>::Inner2;105#if __cplusplus <= 199711L106// expected-warning@-2 {{explicit instantiation of 'Inner2' not in a namespace enclosing 'N2'}}107#else108// expected-error@-4 {{explicit instantiation of 'Inner2' not in a namespace enclosing 'N2'}}109#endif110}111 112struct X6 {113  struct Inner { // expected-note{{here}}114    void f();115  };116};117 118template struct X6::Inner; // expected-error{{non-templated}}119 120// PR5559121template <typename T>122struct Foo;123 124template <>125struct Foo<int> // expected-note{{header not required for explicitly-specialized}}126{127    template <typename U>128    struct Bar129    {};130};131 132template <> // expected-error{{extraneous template parameter list}}133template <>134struct Foo<int>::Bar<void>135{};136 137#if __cplusplus >= 202002L138template<> void f(auto); // expected-error{{extraneous template parameter list}}139#endif140 141namespace N1 {142 143  template<typename T> struct X7 { }; // expected-note{{here}}144 145  namespace Inner {146    template<typename T> struct X8 { };147  }148 149  template struct X7<int>;150  template struct Inner::X8<int>;151}152 153template<typename T> struct X9 { }; // expected-note{{here}}154 155template struct ::N1::Inner::X8<float>;156 157namespace N2 {158  using namespace N1;159 160  template struct X7<double>;161#if __cplusplus <= 199711L162// expected-warning@-2 {{explicit instantiation of 'N1::X7' must occur in namespace 'N1'}}163#else164// expected-error@-4 {{explicit instantiation of 'N1::X7' must occur in namespace 'N1'}}165#endif166 167  template struct X9<float>;168#if __cplusplus <= 199711L169// expected-warning@-2 {{explicit instantiation of 'X9' must occur at global scope}}170#else171// expected-error@-4 {{explicit instantiation of 'X9' must occur at global scope}}172#endif173}174