Clang Project

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