xref: /llvm-project/clang/test/SemaTemplate/temp_arg_template.cpp (revision e29c085812e259910a3d8b6c2d2f471d1c3eede4)
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,precxx17 %std_cxx98-14 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx17 -std=c++17 %s
3 
4 template<template<typename T> class X> struct A; // #A
5 // expected-note@-1 2{{previous template template parameter is here}}
6 
7 template<template<typename T, int I> class X> struct B; // expected-note{{previous template template parameter is here}}
8 
9 template<template<int I> class X> struct C;
10 // expected-error@-1 {{conversion from 'int' to 'const int &' in converted constant expression would bind reference to a temporary}}
11 // expected-note@-2 {{previous template template parameter is here}}
12 
13 template<class> struct X; // expected-note {{template is declared here}}
14 template<int N> struct Y; // expected-note {{template parameter is declared here}}
15 template<long N> struct Ylong;
16 template<const int &N> struct Yref; // expected-note {{template parameter is declared here}}
17 
18 namespace N {
19   template<class> struct Z;
20 }
21 template<class, class> struct TooMany; // expected-note{{template is declared here}}
22 
23 
24 A<X> *a1;
25 A<N::Z> *a2;
26 A< ::N::Z> *a3;
27 
28 A<Y> *a4; // expected-error@#A {{template argument for non-type template parameter must be an expression}}
29           // expected-note@-1 {{different template parameters}}
30 A<TooMany> *a5; // expected-error {{too few template arguments for class template 'TooMany'}}
31                 // expected-note@-1 {{different template parameters}}
32 B<X> *a6; // expected-error {{too many template arguments for class template 'X'}}
33           // expected-note@-1 {{different template parameters}}
34 C<Y> *a7;
35 C<Ylong> *a8;
36 C<Yref> *a9; // expected-note {{different template parameters}}
37 
38 template<typename T> void f(int);
39 
40 A<f> *a9; // expected-error{{must be a class template}}
41 
42 // Evil digraph '<:' is parsed as '[', expect error.
43 A<::N::Z> *a10;
44 #if __cplusplus <= 199711L
45 // expected-error@-2 {{found '<::' after a template name which forms the digraph '<:' (aka '[') and a ':', did you mean '< ::'?}}
46 #endif
47 
48 // Do not do a digraph correction here.
49 A<: :N::Z> *a11;  // expected-error{{expected expression}} \
50                      precxx17-error{{a type specifier is required for all declarations}} \
51                      cxx17-error{{expected unqualified-id}}
52 
53 // PR7807
54 namespace N {
55   template <typename, typename = int>
56   struct X
57   { };
58 
59   template <typename ,int>
60   struct Y
61   { X<int> const_ref(); };
62 
63   template <template<typename,int> class TT, typename T, int N>
64   int operator<<(int, TT<T, N> a) { // expected-note{{candidate template ignored}}
65     0 << a.const_ref(); // expected-error{{invalid operands to binary expression ('int' and 'X<int>')}}
66   }
67 
68   void f0( Y<int,1> y){ 1 << y; } // expected-note{{in instantiation of function template specialization 'N::operator<<<N::Y, int, 1>' requested here}}
69 }
70 
71 // PR12179
72 template <typename Primitive, template <Primitive...> class F>
73 #if __cplusplus <= 199711L
74 // expected-warning@-2 {{variadic templates are a C++11 extension}}
75 #endif
76 
77 struct unbox_args {
78   typedef typename Primitive::template call<F> x;
79 };
80 
81 template <template <typename> class... Templates>
82 #if __cplusplus <= 199711L
83 // expected-warning@-2 {{variadic templates are a C++11 extension}}
84 #endif
85 
86 struct template_tuple {
87 #if __cplusplus >= 201103L
88   static constexpr int N = sizeof...(Templates);
89 #endif
90 };
91 template <typename T>
92 struct identity {};
93 template <template <typename> class... Templates>
94 #if __cplusplus <= 199711L
95 // expected-warning@-2 {{variadic templates are a C++11 extension}}
96 #endif
97 
98 template_tuple<Templates...> f7() {}
99 
100 #if __cplusplus >= 201103L
101 struct S : public template_tuple<identity, identity> {
102   static_assert(N == 2, "Number of template arguments incorrect");
103 };
104 #endif
105 
106 void foo() {
107   f7<identity>();
108 }
109 
110 namespace CheckDependentNonTypeParamTypes {
111   template<template<typename T, typename U, T v> class X> struct A {
112     // expected-note@-1 {{previous template template parameter is here}}
113     void f() {
114       X<int, void*, 3> x;
115     }
116     void g() {
117       X<int, long, 3> x;
118     }
119     void h() {
120       // FIXME: If we accept A<B> at all, it's not obvious what should happen
121       // here. While parsing the template, we form
122       //   X<unsigned char, int, (unsigned char)1234>
123       // but in the final instantiation do we get
124       //   B<unsigned char, int, (int)1234>
125       // or
126       //   B<unsigned char, int, (int)(unsigned char)1234>
127       // ?
128       X<unsigned char, int, 1234> x;
129       int check[x.value == 1234 ? 1 : -1];
130     }
131   };
132 
133   template<typename T, typename U, U v> struct B {
134     // expected-error@-1 {{conflicting deduction 'U' against 'T' for parameter}}
135     static const U value = v;
136   };
137 
138   // FIXME: This should probably be rejected, but the rules are at best unclear.
139   A<B> ab; // expected-note {{different template parameters}}
140 
141   void use() {
142     ab.f();
143     ab.g();
144     ab.h();
145   }
146 }
147 
148 namespace PR32185 {
149   template<template<typename T, T> class U> struct A {};
150   template<template<typename T, T> class U> struct B : A<U> {};
151 }
152 
153 namespace PR10147 {
154   template<typename T> struct A {};
155   template<typename T = int> struct A;
156   template<template<typename...> class A> void f(A<int>*) { A<> a; } // expected-warning 0-1{{extension}}
157   void g() { f((A<>*)0); }
158 }
159