xref: /llvm-project/clang/test/SemaTemplate/temp-param-subst-linear.cpp (revision 5d3310208ae811396ca0cf256f96d4234ce5ae75)
1*5d331020SRichard Smith // RUN: %clang_cc1 -std=c++17 %s -verify
2*5d331020SRichard Smith // expected-no-diagnostics
3*5d331020SRichard Smith 
4*5d331020SRichard Smith // This test attempts to ensure that the below template parameter pack
5*5d331020SRichard Smith // splitting technique executes in linear time in the number of template
6*5d331020SRichard Smith // parameters. The size of the list below is selected so as to execute
7*5d331020SRichard Smith // relatively quickly on a "good" compiler and to time out otherwise.
8*5d331020SRichard Smith 
9*5d331020SRichard Smith template<typename...> struct TypeList;
10*5d331020SRichard Smith 
11*5d331020SRichard Smith namespace detail {
12*5d331020SRichard Smith template<unsigned> using Unsigned = unsigned;
13*5d331020SRichard Smith template<typename T, T ...N> using ListOfNUnsignedsImpl = TypeList<Unsigned<N>...>;
14*5d331020SRichard Smith template<unsigned N> using ListOfNUnsigneds =
15*5d331020SRichard Smith   __make_integer_seq<ListOfNUnsignedsImpl, unsigned, N>;
16*5d331020SRichard Smith 
17*5d331020SRichard Smith template<typename T> struct TypeWrapper {
18*5d331020SRichard Smith   template<unsigned> using AsTemplate = T;
19*5d331020SRichard Smith };
20*5d331020SRichard Smith 
21*5d331020SRichard Smith template<typename ...N> struct Splitter {
22*5d331020SRichard Smith   template<template<N> class ...L,
23*5d331020SRichard Smith            template<unsigned> class ...R> struct Split {
24*5d331020SRichard Smith     using Left = TypeList<L<0>...>;
25*5d331020SRichard Smith     using Right = TypeList<R<0>...>;
26*5d331020SRichard Smith   };
27*5d331020SRichard Smith };
28*5d331020SRichard Smith }
29*5d331020SRichard Smith 
30*5d331020SRichard Smith template<typename TypeList, unsigned N, typename = detail::ListOfNUnsigneds<N>>
31*5d331020SRichard Smith struct SplitAtIndex;
32*5d331020SRichard Smith 
33*5d331020SRichard Smith template<typename ...T, unsigned N, typename ...NUnsigneds>
34*5d331020SRichard Smith struct SplitAtIndex<TypeList<T...>, N, TypeList<NUnsigneds...>> :
35*5d331020SRichard Smith   detail::Splitter<NUnsigneds...>::
36*5d331020SRichard Smith     template Split<detail::TypeWrapper<T>::template AsTemplate...> {};
37*5d331020SRichard Smith 
38*5d331020SRichard Smith template<typename T, int N> struct Rep : Rep<typename Rep<T, N-1>::type, 1> {};
39*5d331020SRichard Smith template<typename ...T> struct Rep<TypeList<T...>, 1> { typedef TypeList<T..., T...> type; };
40*5d331020SRichard Smith 
41*5d331020SRichard Smith using Ints = Rep<TypeList<int>, 14>::type;
42*5d331020SRichard Smith 
43*5d331020SRichard Smith template<typename T> extern int Size;
44*5d331020SRichard Smith template<typename ...T> constexpr int Size<TypeList<T...>> = sizeof...(T);
45*5d331020SRichard Smith 
46*5d331020SRichard Smith using Left = SplitAtIndex<Ints, Size<Ints> / 2>::Left;
47*5d331020SRichard Smith using Right = SplitAtIndex<Ints, Size<Ints> / 2>::Right;
48*5d331020SRichard Smith static_assert(Size<Left> == 8192);
49*5d331020SRichard Smith static_assert(Size<Right> == 8192);
50*5d331020SRichard Smith 
51*5d331020SRichard Smith template<typename L, typename R> struct Concat;
52*5d331020SRichard Smith template<typename ...L, typename ...R> struct Concat<TypeList<L...>, TypeList<R...>> {
53*5d331020SRichard Smith   using type = TypeList<L..., R...>;
54*5d331020SRichard Smith };
55*5d331020SRichard Smith 
56*5d331020SRichard Smith using Ints = Concat<Left, Right>::type;
57