474 lines · c
1// Header for PCH test cxx-templates.cpp2 3template <typename T1, typename T2>4struct S;5 6template <typename T1, typename T2>7struct S {8 S() { }9 static void templ();10};11 12template <typename T>13struct S<int, T> {14 static void partial();15};16 17template <>18struct S<int, float> {19 static void explicit_special();20};21 22template <int x>23int tmpl_f2() { return x; }24 25template <typename T, int y>26T templ_f(T x) {27 int z = templ_f<int, 5>(3);28 z = tmpl_f2<y+2>();29 T data[y];30 return x+y;31}32 33void govl(int);34void govl(char);35 36template <typename T>37struct Unresolv {38 void f() {39 govl(T());40 }41};42 43template <typename T>44struct Dep {45 typedef typename T::type Ty;46 void f() {47 Ty x = Ty();48 T::my_f();49 int y = T::template my_templf<int>(0);50 ovl(y);51 }52 53 void ovl(int);54 void ovl(float);55};56 57template<typename T, typename A1>58inline T make_a(const A1& a1) {59 T::depend_declref();60 return T(a1);61}62 63template <class T> class UseBase {64 void foo();65 typedef int bar;66};67 68template <class T> class UseA : public UseBase<T> {69 using UseBase<T>::foo;70 using typename UseBase<T>::bar;71};72 73template <class T> class Sub : public UseBase<int> { };74 75template <class _Ret, class _Tp>76 class mem_fun_t77 {78 public:79 explicit80 mem_fun_t(_Ret (_Tp::*__pf)())81 {}82 83 private:84 _Ret (_Tp::*_M_f)();85 };86 87template<unsigned N>88bool isInt(int x);89 90template<> bool isInt<8>(int x) {91 try { ++x; } catch(...) { --x; }92 return true;93}94 95template<typename _CharT>96int __copy_streambufs_eof(_CharT);97 98class basic_streambuf99{100 void m() { }101 friend int __copy_streambufs_eof<>(int);102};103 104// PR 7660105template<typename T> struct S_PR7660 { void g(void (*)(T)); };106 template<> void S_PR7660<int>::g(void(*)(int)) {}107 108// PR 7670109template<typename> class C_PR7670;110template<> class C_PR7670<int>;111template<> class C_PR7670<int>;112 113template <bool B>114struct S2 {115 static bool V;116};117 118extern template class S2<true>;119 120template <typename T>121struct S3 {122 void m();123};124 125template <typename T>126inline void S3<T>::m() { }127 128template <typename T>129struct S4 {130 void m() { }131};132extern template struct S4<int>;133 134void S4ImplicitInst() {135 S4<int> s;136 s.m();137}138 139struct S5 {140 S5(int x);141};142 143struct TS5 {144 S5 s;145 template <typename T>146 TS5(T y) : s(y) {}147};148 149// PR 8134150template<class T> void f_PR8134(T);151template<class T> void f_PR8134(T);152void g_PR8134() { f_PR8134(0); f_PR8134('x'); }153 154// rdar8580149155template <typename T>156struct S6;157 158template <typename T, unsigned N>159struct S6<const T [N]>160{161private:162 typedef const T t1[N];163public:164 typedef t1& t2;165};166 167template<typename T>168 struct S7;169 170template<unsigned N>171struct S7<int[N]> : S6<const int[N]> { };172 173// Zero-length template argument lists174namespace ZeroLengthExplicitTemplateArgs {175 template<typename T> void h();176 177 struct Y {178 template<typename T> void f();179 };180 181 template<typename T>182 void f(T *ptr) {183 T::template g<>(17);184 ptr->template g2<>(17);185 h<T>();186 h<int>();187 Y y;188 y.f<int>();189 }190 191 struct X {192 template<typename T> static void g(T);193 template<typename T> void g2(T);194 };195}196 197namespace NonTypeTemplateParmContext {198 template<typename T, int inlineCapacity = 0> class Vector { };199 200 struct String {201 template<int inlineCapacity>202 static String adopt(Vector<char, inlineCapacity>&);203 };204 205 template<int inlineCapacity>206 inline bool equalIgnoringNullity(const Vector<char, inlineCapacity>& a, const String& b) { return false; }207}208 209template< typename > class Foo;210 211template< typename T >212class Foo : protected T213{214 public:215 Foo& operator=( const Foo& other );216};217 218template<typename...A> struct NestedExpansion {219 template<typename...B> auto f(A...a, B...b) -> decltype(g(a + b...));220};221template struct NestedExpansion<char, char, char>;222 223namespace rdar13135282 {224template < typename _Alloc >225void foo(_Alloc = _Alloc());226 227template < bool > class __pool;228 229template < template < bool > class _PoolTp >230struct __common_pool {231 typedef _PoolTp < 0 > pool_type;232};233 234template < template < bool > class _PoolTp >235struct __common_pool_base : __common_pool < _PoolTp > {};236 237template < template < bool > class _PoolTp >238struct A : __common_pool_base < _PoolTp > {};239 240template < typename _Poolp = A < __pool > >241struct __mt_alloc {242 typedef typename _Poolp::pool_type __pool_type;243 __mt_alloc() {244 foo<__mt_alloc<> >();245 }246};247}248 249namespace PR13020 {250template<typename T>251void f() {252 enum E {253 enumerator254 };255 256 T t = enumerator;257}258 259template void f<int>();260}261 262template<typename T> void doNotDeserialize() {}263template<typename T> struct ContainsDoNotDeserialize {264 static int doNotDeserialize;265};266template<typename T> struct ContainsDoNotDeserialize2 {267 static void doNotDeserialize();268};269template<typename T> int ContainsDoNotDeserialize<T>::doNotDeserialize = 0;270template<typename T> void ContainsDoNotDeserialize2<T>::doNotDeserialize() {}271 272 273template<typename T> void DependentSpecializedFunc(T x) { x.foo(); }274template<typename T> class DependentSpecializedFuncClass {275 void foo() {}276 friend void DependentSpecializedFunc<>(DependentSpecializedFuncClass);277};278 279namespace cyclic_module_load {280 // Reduced from a libc++ modules crasher.281 namespace std {282 template<class> class mask_array;283 template<class> class valarray {284 public:285 valarray(const valarray &v);286 };287 288 class gslice {289 valarray<int> x;290 valarray<int> stride() const { return x; }291 };292 293 template<class> class mask_array {294 template<class> friend class valarray;295 };296 }297}298 299namespace local_extern {300 template<typename T> int f() {301 extern int arr[3];302 {303 extern T arr;304 return sizeof(arr);305 }306 }307 template<typename T> int g() {308 extern int arr[3];309 extern T arr;310 return sizeof(arr);311 }312}313 314namespace rdar15468709a {315 template<typename> struct decay {};316 317 template<typename FooParamTy> auto foo(FooParamTy fooParam) -> decltype(fooParam);318 template<typename BarParamTy> auto bar(BarParamTy barParam) -> decay<decltype(barParam)>;319 320 struct B {};321 322 void crash() {323 B some;324 bar(some);325 }326}327 328namespace rdar15468709b {329 template<typename> struct decay {};330 331 template<typename... Foos> int returnsInt(Foos... foos);332 333 template<typename... FooParamTy> auto foo(FooParamTy... fooParam) -> decltype(returnsInt(fooParam...));334 template<typename... BarParamTy> auto bar(BarParamTy... barParam) -> decay<decltype(returnsInt(barParam...))>;335 336 struct B {};337 338 void crash() {339 B some;340 bar(some);341 }342}343 344namespace rdar15468709c {345 template<typename> struct decay {};346 347 template<class... Foos> int returnsInt(Foos... foos);348 349 template<typename FooParamTy> void foo(FooParamTy fooParam) { decltype(fooParam) a; }350 template<typename BarParamTy> auto bar(BarParamTy barParam) -> decay<decltype(barParam)>;351 352 struct B {};353 354 void crash() {355 B some;356 bar(some);357 }358}359 360namespace MemberSpecializationLocation {361 template<typename T> struct A { static int n; };362}363 364// https://bugs.llvm.org/show_bug.cgi?id=34728365namespace PR34728 {366 367// case 1: defaulted `NonTypeTemplateParmDecl`, non-defaulted 2nd tpl param368template <int foo = 10, class T>369int func1(T const &);370 371template <int foo, class T>372int func1(T const &) {373 return foo;374}375 376// case 2: defaulted `TemplateTypeParmDecl`, non-defaulted 2nd tpl param377template <class A = int, class B>378A func2(B const &);379 380template <class A, class B>381A func2(B const &) {382 return A(20.0f);383}384 385// case 3: defaulted `TemplateTemplateParmDecl`, non-defaulted 2nd tpl param386template <class T>387struct Container { T const &item; };388 389template <template <class> class C = Container, class D>390C<D> func3(D const &);391 392template <template <class> class C, class D>393C<D> func3(D const &d) {394 return Container<D>{d};395}396 397} // end namespace PR34728398 399namespace ClassScopeExplicitSpecializations {400 template<int> struct A {401 template<int> constexpr int f() const { return 1; }402 template<> constexpr int f<0>() const { return 2; }403 };404 405 template<> template<int> constexpr int A<0>::f() const { return 3; }406 template<> template<> constexpr int A<0>::f<0>() const { return 4; }407 template<> template<> constexpr int A<0>::f<1>() const { return 5; }408 409#pragma clang diagnostic push410#pragma clang diagnostic ignored "-Winstantiation-after-specialization"411 template int A<2>::f<0>() const;412#pragma clang diagnostic pop413 template int A<2>::f<1>() const;414 extern template int A<3>::f<0>() const;415 extern template int A<3>::f<1>() const;416 417 template<int> struct B {418 template<typename> static const int v = 1;419 template<typename T> static const int v<T*> = 2;420 template<> const int v<int> = 3;421 422 template<typename> static constexpr int w = 1;423 template<typename T> static constexpr int w<T*> = 2;424 template<> constexpr int w<int> = 3;425 };426 427 template<> template<typename> constexpr int B<0>::v = 4;428 template<> template<typename T> constexpr int B<0>::v<T*> = 5;429 template<> template<typename T> constexpr int B<0>::v<T&> = 6;430 // This is ill-formed: the initializer of v<int> is instantiated with the431 // class.432 //template<> template<> constexpr int B<0>::v<int> = 7;433 template<> template<> constexpr int B<0>::v<float> = 8;434 435 template<> template<typename> constexpr int B<0>::w = 4;436 template<> template<typename T> constexpr int B<0>::w<T*> = 5;437 template<> template<typename T> constexpr int B<0>::w<T&> = 6;438 template<> template<> constexpr int B<0>::w<int> = 7;439 template<> template<> constexpr int B<0>::w<float> = 8;440}441 442namespace DependentMemberExpr {443 struct Base {444 constexpr int setstate() { return 0; }445 };446 template<typename T> struct A : Base {447 constexpr int f() { return Base::setstate(); }448 };449}450 451namespace DependentTemplateName {452 template <template <class> class Template>453 struct TakesClassTemplate {};454 455 template <class T>456 TakesClassTemplate<T::template Member> getWithIdentifier();457}458 459namespace ClassTemplateCycle {460 // Create a cycle: the typedef T refers to A<0, 8>, whose template argument461 // list refers back to T.462 template<int, int> struct A;463 using T = A<0, sizeof(void*)>;464 template<int N> struct A<N, sizeof(T*)> {};465 T t;466 467 // Create a cycle: the variable M refers to A<1, 1>, whose template argument468 // list list refers back to M.469 template<int, int> struct A;470 const decltype(sizeof(A<1, 1>*)) M = 1;471 template<int N> struct A<N, M> {};472 A<1, 1> u;473}474