explicit-instantiation.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // RUN: %clang_cc1 -fsyntax-only -verify -fexceptions -fcxx-exceptions %s
  2. // RUN: %clang_cc1 -fsyntax-only -verify -fexceptions -fcxx-exceptions -std=c++11 %s
  3. template void *; // expected-error{{expected unqualified-id}}
  4. template typedef void f0; // expected-error{{explicit instantiation of typedef}}
  5. int v0; // expected-note{{refers here}}
  6. template int v0; // expected-error{{does not refer}}
  7. template<typename T>
  8. struct X0 {
  9. static T value;
  10. T f0(T x) {
  11. return x + 1; // expected-error{{invalid operands}}
  12. }
  13. T *f0(T *, T *) { return T(); } // expected-warning 0-1 {{expression which evaluates to zero treated as a null pointer constant of type 'int *'}} expected-error 0-1 {{cannot initialize return object of type 'int *' with an rvalue of type 'int'}}
  14. template <typename U> T f0(T, U) { return T(); } // expected-note-re {{candidate template ignored: could not match 'int (int, U){{( __attribute__\(\(thiscall\)\))?}}' against 'int (int){{( __attribute__\(\(thiscall\)\))?}} const'}} \
  15. // expected-note {{candidate template ignored: could not match 'int' against 'int *'}}
  16. };
  17. template<typename T>
  18. T X0<T>::value; // expected-error{{no matching constructor}}
  19. template int X0<int>::value;
  20. struct NotDefaultConstructible { // expected-note{{candidate constructor (the implicit copy constructor)}} expected-note 0-1 {{candidate constructor (the implicit move constructor)}}
  21. NotDefaultConstructible(int); // expected-note{{candidate constructor}}
  22. };
  23. template NotDefaultConstructible X0<NotDefaultConstructible>::value; // expected-note{{instantiation}}
  24. template int X0<int>::f0(int);
  25. template int* X0<int>::f0(int*, int*); // expected-note{{in instantiation of member function 'X0<int>::f0' requested here}}
  26. template int X0<int>::f0(int, float);
  27. template int X0<int>::f0(int) const; // expected-error{{does not refer}}
  28. template int* X0<int>::f0(int*, float*); // expected-error{{does not refer}}
  29. struct X1 { };
  30. typedef int X1::*MemPtr;
  31. template MemPtr X0<MemPtr>::f0(MemPtr); // expected-note{{requested here}}
  32. struct X2 {
  33. int f0(int); // expected-note{{refers here}}
  34. template<typename T> T f1(T) { return T(); }
  35. template<typename T> T* f1(T*) { return 0; }
  36. template<typename T, typename U> void f2(T, U*) { } // expected-note{{candidate}}
  37. template<typename T, typename U> void f2(T*, U) { } // expected-note{{candidate}}
  38. };
  39. template int X2::f0(int); // expected-error{{not an instantiation}}
  40. template int *X2::f1(int *); // okay
  41. template void X2::f2(int *, int *); // expected-error{{ambiguous}}
  42. template <typename T>
  43. void print_type() {} // expected-note {{candidate template ignored: could not match 'void ()' against 'void (float *)'}}
  44. template void print_type<int>();
  45. template void print_type<float>();
  46. template <typename T>
  47. void print_type(T *) {} // expected-note {{candidate template ignored: could not match 'void (int *)' against 'void (float *)'}}
  48. template void print_type(int*);
  49. template void print_type<int>(float*); // expected-error{{does not refer}}
  50. void print_type(double*);
  51. template void print_type<double>(double*);
  52. // PR5069
  53. template<int I> void foo0 (int (&)[I + 1]) { }
  54. template void foo0<2> (int (&)[3]);
  55. namespace explicit_instantiation_after_implicit_instantiation {
  56. template <int I> struct X0 { static int x; };
  57. template <int I> int X0<I>::x;
  58. void test1() { (void)&X0<1>::x; }
  59. template struct X0<1>;
  60. }
  61. template<typename> struct X3 { };
  62. inline template struct X3<int>; // expected-warning{{ignoring 'inline' keyword on explicit template instantiation}}
  63. static template struct X3<float>; // expected-warning{{ignoring 'static' keyword on explicit template instantiation}}
  64. namespace PR7622 {
  65. template<typename,typename=int>
  66. struct basic_streambuf;
  67. template<typename,typename>
  68. struct basic_streambuf{friend bob<>()}; // expected-error{{unknown type name 'bob'}} \
  69. // expected-error{{expected member name or ';' after declaration specifiers}}
  70. template struct basic_streambuf<int>;
  71. }
  72. // Test that we do not crash.
  73. class TC1 {
  74. class TC2 {
  75. template // FIXME: error here.
  76. void foo() { }
  77. };
  78. };
  79. namespace PR8020 {
  80. template <typename T> struct X { X() {} };
  81. template<> struct X<int> { X(); };
  82. template X<int>::X() {} // expected-error{{function cannot be defined in an explicit instantiation}}
  83. }
  84. namespace PR10086 {
  85. template void foobar(int i) {} // expected-error{{function cannot be defined in an explicit instantiation}}
  86. int func() {
  87. foobar(5);
  88. }
  89. }
  90. namespace undefined_static_data_member {
  91. template<typename T> struct A {
  92. static int a; // expected-note {{here}}
  93. template<typename U> static int b; // expected-note {{here}} expected-warning {{extension}}
  94. };
  95. struct B {
  96. template<typename U> static int c; // expected-note {{here}} expected-warning {{extension}}
  97. };
  98. template int A<int>::a; // expected-error {{explicit instantiation of undefined static data member 'a' of class template 'undefined_static_data_member::A<int>'}}
  99. template int A<int>::b<int>; // expected-error {{explicit instantiation of undefined variable template 'undefined_static_data_member::A<int>::b<int>'}}
  100. template int B::c<int>; // expected-error {{explicit instantiation of undefined variable template 'undefined_static_data_member::B::c<int>'}}
  101. template<typename T> struct C {
  102. static int a;
  103. template<typename U> static int b; // expected-warning {{extension}}
  104. };
  105. struct D {
  106. template<typename U> static int c; // expected-warning {{extension}}
  107. };
  108. template<typename T> int C<T>::a;
  109. template<typename T> template<typename U> int C<T>::b; // expected-warning {{extension}}
  110. template<typename U> int D::c; // expected-warning {{extension}}
  111. template int C<int>::a;
  112. template int C<int>::b<int>;
  113. template int D::c<int>;
  114. }
  115. // expected-note@+1 3-4 {{explicit instantiation refers here}}
  116. template <class T> void Foo(T i) throw(T) { throw i; }
  117. // expected-error@+1 {{exception specification in explicit instantiation does not match instantiated one}}
  118. template void Foo(int a) throw(char);
  119. // expected-error@+1 {{exception specification in explicit instantiation does not match instantiated one}}
  120. template void Foo(double a) throw();
  121. // expected-error@+1 1 {{exception specification in explicit instantiation does not match instantiated one}}
  122. template void Foo(long a) throw(long, char);
  123. template void Foo(float a);
  124. #if __cplusplus >= 201103L
  125. // expected-error@+1 0-1 {{exception specification in explicit instantiation does not match instantiated one}}
  126. template void Foo(double a) noexcept;
  127. #endif
  128. #if __cplusplus >= 201103L
  129. namespace PR21942 {
  130. template <int>
  131. struct A {
  132. virtual void foo() final;
  133. };
  134. template <>
  135. void A<0>::foo() {} // expected-note{{overridden virtual function is here}}
  136. struct B : A<0> {
  137. virtual void foo() override; // expected-error{{declaration of 'foo' overrides a 'final' function}}
  138. };
  139. }
  140. #endif