1 // RUN: %clang_cc1 -fsyntax-only -verify -triple=x86_64-linux-gnu %s 2 // RUN: %clang_cc1 -fsyntax-only -verify -triple=x86_64-linux-gnu -std=c++98 %s 3 // RUN: %clang_cc1 -fsyntax-only -verify -triple=x86_64-linux-gnu -std=c++11 %s 4 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -triple=x86_64-linux-gnu %s -DCPP11ONLY 5 6 // C++11 [temp.arg.nontype]p1: 7 // 8 // A template-argument for a non-type, non-template template-parameter shall 9 // be one of: 10 // -- an integral constant expression; or 11 // -- the name of a non-type template-parameter ; or 12 #ifndef CPP11ONLY 13 14 namespace non_type_tmpl_param { 15 template <int N> struct X0 { X0(); }; X0()16 template <int N> X0<N>::X0() { } 17 template <int* N> struct X1 { X1(); }; X1()18 template <int* N> X1<N>::X1() { } 19 template <int& N> struct X3 { X3(); }; X3()20 template <int& N> X3<N>::X3() { } 21 template <int (*F)(int)> struct X4 { X4(); }; X4()22 template <int (*F)(int)> X4<F>::X4() { } 23 template <typename T, int (T::* M)(int)> struct X5 { X5(); }; X5()24 template <typename T, int (T::* M)(int)> X5<T, M>::X5() { } 25 } 26 27 // -- a constant expression that designates the address of an object with 28 // static storage duration and external or internal linkage or a function 29 // with external or internal linkage, including function templates and 30 // function template-ids, but excluting non-static class members, expressed 31 // (ignoring parentheses) as & id-expression, except that the & may be 32 // omitted if the name refers to a function or array and shall be omitted 33 // if the corresopnding template-parameter is a reference; or 34 namespace addr_of_obj_or_func { 35 template <int* p> struct X0 { }; // expected-note 5{{here}} 36 #if __cplusplus >= 201103L 37 // expected-note@-2 2{{template parameter is declared here}} 38 #endif 39 40 template <int (*fp)(int)> struct X1 { }; 41 template <int &p> struct X2 { }; // expected-note 4{{here}} 42 template <const int &p> struct X2k { }; // expected-note {{here}} 43 template <int (&fp)(int)> struct X3 { }; // expected-note 4{{here}} 44 45 int i = 42; 46 #if __cplusplus >= 201103L 47 // expected-note@-2 {{declared here}} 48 #endif 49 50 int iarr[10]; 51 int f(int i); 52 const int ki = 9; 53 #if __cplusplus <= 199711L 54 // expected-note@-2 5{{non-type template argument refers to object here}} 55 #endif 56 57 __thread int ti = 100; // expected-note {{here}} 58 #if __cplusplus <= 199711L 59 // expected-note@-2 {{here}} 60 #endif 61 62 static int f_internal(int); 63 #if __cplusplus <= 199711L 64 // expected-note@-2 4{{non-type template argument refers to function here}} 65 #endif 66 67 template <typename T> T f_tmpl(T t); 68 struct S { union { int NonStaticMember; }; }; 69 test()70 void test() { 71 X0<i> x0a; 72 #if __cplusplus <= 199711L 73 // expected-error@-2 {{non-type template argument for template parameter of pointer type 'int *' must have its address taken}} 74 #else 75 // expected-error@-4 {{non-type template argument of type 'int' is not a constant expression}} 76 // expected-note@-5 {{read of non-const variable 'i' is not allowed in a constant expression}} 77 #endif 78 X0<&i> x0a_addr; 79 X0<iarr> x0b; 80 X0<&iarr> x0b_addr; // expected-error {{cannot be converted to a value of type 'int *'}} 81 X0<ki> x0c; // expected-error {{must have its address taken}} 82 #if __cplusplus <= 199711L 83 // expected-warning@-2 {{internal linkage is a C++11 extension}} 84 #endif 85 86 X0<&ki> x0c_addr; // expected-error {{cannot be converted to a value of type 'int *'}} 87 #if __cplusplus <= 199711L 88 // expected-warning@-2 {{internal linkage is a C++11 extension}} 89 #endif 90 91 X0<&ti> x0d_addr; 92 #if __cplusplus <= 199711L 93 // expected-error@-2 {{non-type template argument refers to thread-local object}} 94 #else 95 // expected-error@-4 {{non-type template argument of type 'int *' is not a constant expression}} 96 #endif 97 98 X1<f> x1a; 99 X1<&f> x1a_addr; 100 X1<f_tmpl> x1b; 101 X1<&f_tmpl> x1b_addr; 102 X1<f_tmpl<int> > x1c; 103 X1<&f_tmpl<int> > x1c_addr; 104 X1<f_internal> x1d; 105 #if __cplusplus <= 199711L 106 // expected-warning@-2 {{internal linkage is a C++11 extension}} 107 #endif 108 109 X1<&f_internal> x1d_addr; 110 #if __cplusplus <= 199711L 111 // expected-warning@-2 {{internal linkage is a C++11 extension}} 112 #endif 113 114 X2<i> x2a; 115 X2<&i> x2a_addr; // expected-error {{address taken}} 116 X2<iarr> x2b; // expected-error {{cannot bind to template argument of type 'int [10]'}} 117 X2<&iarr> x2b_addr; // expected-error {{address taken}} 118 X2<ki> x2c; // expected-error {{ignores qualifiers}} 119 #if __cplusplus <= 199711L 120 // expected-warning@-2 {{internal linkage is a C++11 extension}} 121 #endif 122 123 X2k<ki> x2kc; 124 #if __cplusplus <= 199711L 125 // expected-warning@-2 {{internal linkage is a C++11 extension}} 126 #endif 127 128 X2k<&ki> x2kc_addr; // expected-error {{address taken}} 129 #if __cplusplus <= 199711L 130 // expected-warning@-2 {{internal linkage is a C++11 extension}} 131 #endif 132 133 X2<ti> x2d_addr; // expected-error {{refers to thread-local object}} 134 X3<f> x3a; 135 X3<&f> x3a_addr; // expected-error {{address taken}} 136 X3<f_tmpl> x3b; 137 X3<&f_tmpl> x3b_addr; // expected-error {{address taken}} 138 X3<f_tmpl<int> > x3c; 139 X3<&f_tmpl<int> > x3c_addr; // expected-error {{address taken}} 140 X3<f_internal> x3d; 141 #if __cplusplus <= 199711L 142 // expected-warning@-2 {{internal linkage is a C++11 extension}} 143 #endif 144 145 X3<&f_internal> x3d_addr; // expected-error {{address taken}} 146 #if __cplusplus <= 199711L 147 // expected-warning@-2 {{internal linkage is a C++11 extension}} 148 #endif 149 150 int n; 151 #if __cplusplus <= 199711L 152 // expected-note@-2 {{non-type template argument refers to object here}} 153 #else 154 // expected-note@-4 {{declared here}} 155 #endif 156 157 X0<&n> x0_no_linkage; 158 #if __cplusplus <= 199711L 159 // expected-error@-2 {{non-type template argument refers to object 'n' that does not have linkage}} 160 #else 161 // expected-error@-4 {{non-type template argument of type 'int *' is not a constant expression}} 162 // expected-note@-5 {{pointer to 'n' is not a constant expression}} 163 #endif 164 165 struct Local { static int f() {} }; // expected-note {{here}} 166 X1<&Local::f> x1_no_linkage; // expected-error {{non-type template argument refers to function 'f' that does not have linkage}} 167 X0<&S::NonStaticMember> x0_non_static; // expected-error {{non-static data member}} 168 } 169 } 170 171 // -- a constant expression that evaluates to a null pointer value (4.10); or 172 // -- a constant expression that evaluates to a null member pointer value 173 // (4.11); or 174 // -- a pointer to member expressed as described in 5.3.1. 175 176 namespace bad_args { 177 template <int* N> struct X0 { }; // expected-note 2{{template parameter is declared here}} 178 int i = 42; 179 X0<&i + 2> x0a; // expected-error{{non-type template argument does not refer to any declaration}} 180 int* iptr = &i; 181 #if __cplusplus >= 201103L 182 // expected-note@-2 {{declared here}} 183 #endif 184 185 X0<iptr> x0b; 186 #if __cplusplus <= 199711L 187 // expected-error@-2 {{non-type template argument for template parameter of pointer type 'int *' must have its address taken}} 188 #else 189 // expected-error@-4 {{non-type template argument of type 'int *' is not a constant expression}} 190 // expected-note@-5 {{read of non-constexpr variable 'iptr' is not allowed in a constant expression}} 191 #endif 192 } 193 #endif // CPP11ONLY 194 195 namespace default_args { 196 #ifdef CPP11ONLY 197 namespace lambdas { 198 template<int I = ([] { return 5; }())> //expected-error {{constant expression}} 199 int f(); 200 } 201 #endif // CPP11ONLY 202 203 } 204