1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -verify -fsyntax-only -Wno-c++11-extensions -Wno-c++1y-extensions %s -DPRECXX11 2f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -verify -fsyntax-only -Wno-c++1y-extensions %s 3f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++1y -verify -fsyntax-only %s 4f4a2713aSLionel Sambuc 5f4a2713aSLionel Sambuc #ifdef PRECXX11 6f4a2713aSLionel Sambuc #define CONST const 7f4a2713aSLionel Sambuc #else 8f4a2713aSLionel Sambuc #define CONST constexpr 9f4a2713aSLionel Sambuc #endif 10f4a2713aSLionel Sambuc 11f4a2713aSLionel Sambuc template<typename T> 12f4a2713aSLionel Sambuc T pi = T(3.1415926535897932385); // expected-note {{template is declared here}} 13f4a2713aSLionel Sambuc 14f4a2713aSLionel Sambuc template<typename T> 15f4a2713aSLionel Sambuc CONST T cpi = T(3.1415926535897932385); // expected-note {{template is declared here}} 16f4a2713aSLionel Sambuc 17f4a2713aSLionel Sambuc template<typename T> extern CONST T vc; 18f4a2713aSLionel Sambuc #ifndef PRECXX11 19f4a2713aSLionel Sambuc // expected-error@-2 {{constexpr variable declaration must be a definition}} 20f4a2713aSLionel Sambuc #endif 21f4a2713aSLionel Sambuc 22f4a2713aSLionel Sambuc namespace use_in_top_level_funcs { 23f4a2713aSLionel Sambuc good()24f4a2713aSLionel Sambuc void good() { 25f4a2713aSLionel Sambuc int ipi = pi<int>; 26f4a2713aSLionel Sambuc int icpi = cpi<int>; 27f4a2713aSLionel Sambuc double dpi = pi<double>; 28f4a2713aSLionel Sambuc double dcpi = cpi<double>; 29f4a2713aSLionel Sambuc } 30f4a2713aSLionel Sambuc no_deduce()31f4a2713aSLionel Sambuc void no_deduce() { 32f4a2713aSLionel Sambuc // template arguments are not deduced for uses of variable templates. 33f4a2713aSLionel Sambuc int ipi = pi; // expected-error {{cannot refer to variable template 'pi' without a template argument list}} 34f4a2713aSLionel Sambuc int icpi = cpi; // expected-error {{cannot refer to variable template 'cpi' without a template argument list}} 35f4a2713aSLionel Sambuc } 36f4a2713aSLionel Sambuc 37f4a2713aSLionel Sambuc template<typename T> circular_area(T r)38f4a2713aSLionel Sambuc T circular_area(T r) { 39f4a2713aSLionel Sambuc return pi<T> * r * r; 40f4a2713aSLionel Sambuc } 41f4a2713aSLionel Sambuc 42f4a2713aSLionel Sambuc template<typename T> const_circular_area(T r)43f4a2713aSLionel Sambuc CONST T const_circular_area(T r) { 44f4a2713aSLionel Sambuc return cpi<T> * r * r; 45f4a2713aSLionel Sambuc } 46f4a2713aSLionel Sambuc use_circular_area(double r)47f4a2713aSLionel Sambuc double use_circular_area(double r) { 48f4a2713aSLionel Sambuc CONST float t = const_circular_area(2.0) - 12; 49f4a2713aSLionel Sambuc #ifndef PRECXX11 50f4a2713aSLionel Sambuc static_assert(const_circular_area(2) == 12, ""); 51f4a2713aSLionel Sambuc CONST int test = (t > 0) && (t < 1); 52f4a2713aSLionel Sambuc static_assert(test, ""); 53f4a2713aSLionel Sambuc #endif 54f4a2713aSLionel Sambuc return circular_area(r); 55f4a2713aSLionel Sambuc } 56f4a2713aSLionel Sambuc } 57f4a2713aSLionel Sambuc 58f4a2713aSLionel Sambuc namespace shadow { foo()59f4a2713aSLionel Sambuc void foo() { 60f4a2713aSLionel Sambuc int ipi0 = pi<int>; 61f4a2713aSLionel Sambuc int pi; 62f4a2713aSLionel Sambuc int a = pi; 63f4a2713aSLionel Sambuc int ipi = pi<int>; // expected-error {{expected '(' for function-style cast or type construction}} \ 64f4a2713aSLionel Sambuc // expected-error {{expected expression}} 65f4a2713aSLionel Sambuc } 66f4a2713aSLionel Sambuc } 67f4a2713aSLionel Sambuc 68f4a2713aSLionel Sambuc namespace odr_tmpl { 69f4a2713aSLionel Sambuc namespace pv_cvt { 70f4a2713aSLionel Sambuc int v; // expected-note {{previous definition is here}} 71f4a2713aSLionel Sambuc template<typename T> T v; // expected-error {{redefinition of 'v' as different kind of symbol}} 72f4a2713aSLionel Sambuc } 73f4a2713aSLionel Sambuc namespace pvt_cv { 74f4a2713aSLionel Sambuc template<typename T> T v; // expected-note {{previous definition is here}} 75f4a2713aSLionel Sambuc int v; // expected-error {{redefinition of 'v' as different kind of symbol}} 76f4a2713aSLionel Sambuc } 77f4a2713aSLionel Sambuc namespace pvt_cvt { 78f4a2713aSLionel Sambuc template<typename T> T v0; // expected-note {{previous definition is here}} 79f4a2713aSLionel Sambuc template<typename T> T v0; // expected-error {{redefinition of 'v0'}} 80f4a2713aSLionel Sambuc 81f4a2713aSLionel Sambuc template<typename T> T v; // expected-note {{previous definition is here}} 82f4a2713aSLionel Sambuc template<typename T> int v; // expected-error {{redefinition of 'v'}} 83f4a2713aSLionel Sambuc 84*0a6a1f1dSLionel Sambuc template<typename T> extern int v1; // expected-note {{previous template declaration is here}} 85f4a2713aSLionel Sambuc template<int I> int v1; // expected-error {{template parameter has a different kind in template redeclaration}} 86f4a2713aSLionel Sambuc } 87f4a2713aSLionel Sambuc namespace pvt_use { 88f4a2713aSLionel Sambuc template<typename T> T v; 89f4a2713aSLionel Sambuc v = 10; // expected-error {{C++ requires a type specifier for all declarations}} 90f4a2713aSLionel Sambuc } 91f4a2713aSLionel Sambuc 92f4a2713aSLionel Sambuc namespace pvt_diff_params { 93*0a6a1f1dSLionel Sambuc template<typename T, typename> T v; // expected-note {{previous template declaration is here}} 94*0a6a1f1dSLionel Sambuc template<typename T> T v; // expected-error {{too few template parameters in template redeclaration}} expected-note {{previous template declaration is here}} 95f4a2713aSLionel Sambuc template<typename T, typename, typename> T v; // expected-error {{too many template parameters in template redeclaration}} 96f4a2713aSLionel Sambuc } 97f4a2713aSLionel Sambuc 98f4a2713aSLionel Sambuc namespace pvt_extern { 99f4a2713aSLionel Sambuc template<typename T> T v = T(); 100f4a2713aSLionel Sambuc template<typename T> extern T v; // redeclaration is allowed \ 101f4a2713aSLionel Sambuc // expected-note {{previous definition is here}} 102f4a2713aSLionel Sambuc template<typename T> extern int v; // expected-error {{redefinition of 'v' with a different type: 'int' vs 'T'}} 103f4a2713aSLionel Sambuc 104f4a2713aSLionel Sambuc #ifndef PRECXX11 105f4a2713aSLionel Sambuc template<typename T> extern auto v; // expected-error {{declaration of variable 'v' with type 'auto' requires an initializer}} 106f4a2713aSLionel Sambuc #endif 107f4a2713aSLionel Sambuc 108f4a2713aSLionel Sambuc template<typename T> T var = T(); // expected-note {{previous definition is here}} 109f4a2713aSLionel Sambuc extern int var; // expected-error {{redefinition of 'var' as different kind of symbol}} 110f4a2713aSLionel Sambuc } 111f4a2713aSLionel Sambuc 112f4a2713aSLionel Sambuc #ifndef PRECXX11 113f4a2713aSLionel Sambuc namespace pvt_auto { 114f4a2713aSLionel Sambuc template<typename T> auto v0; // expected-error {{declaration of variable 'v0' with type 'auto' requires an initializer}} 115f4a2713aSLionel Sambuc template<typename T> auto v1 = T(); // expected-note {{previous definition is here}} 116f4a2713aSLionel Sambuc template<typename T> int v1; // expected-error {{redefinition of 'v1' with a different type: 'int' vs 'auto'}} 117f4a2713aSLionel Sambuc template<typename T> auto v2 = T(); // expected-note {{previous definition is here}} 118f4a2713aSLionel Sambuc template<typename T> T v2; // expected-error {{redefinition of 'v2'}} 119f4a2713aSLionel Sambuc template<typename T> auto v3 = T(); // expected-note {{previous definition is here}} 120f4a2713aSLionel Sambuc template<typename T> extern T v3; // expected-error {{redefinition of 'v3' with a different type: 'T' vs 'auto'}} 121f4a2713aSLionel Sambuc template<typename T> auto v4 = T(); 122f4a2713aSLionel Sambuc template<typename T> extern auto v4; // expected-error {{declaration of variable 'v4' with type 'auto' requires an initializer}} 123f4a2713aSLionel Sambuc } 124f4a2713aSLionel Sambuc #endif 125f4a2713aSLionel Sambuc 126f4a2713aSLionel Sambuc } 127f4a2713aSLionel Sambuc 128f4a2713aSLionel Sambuc namespace explicit_instantiation { 129f4a2713aSLionel Sambuc template<typename T> 130f4a2713aSLionel Sambuc T pi0a = T(3.1415926535897932385); // expected-note {{variable template 'pi0a' declared here}} 131f4a2713aSLionel Sambuc template float pi0a<int>; // expected-error {{type 'float' of explicit instantiation of 'pi0a' does not match expected type 'int'}} 132f4a2713aSLionel Sambuc 133f4a2713aSLionel Sambuc template<typename T> 134f4a2713aSLionel Sambuc T pi0b = T(3.1415926535897932385); // expected-note {{variable template 'pi0b' declared here}} 135f4a2713aSLionel Sambuc template CONST int pi0b<int>; // expected-error {{type 'const int' of explicit instantiation of 'pi0b' does not match expected type 'int'}} 136f4a2713aSLionel Sambuc 137f4a2713aSLionel Sambuc template<typename T> 138f4a2713aSLionel Sambuc T pi0c = T(3.1415926535897932385); // expected-note {{variable template 'pi0c' declared here}} 139f4a2713aSLionel Sambuc template int pi0c<const int>; // expected-error {{type 'int' of explicit instantiation of 'pi0c' does not match expected type 'const int'}} 140f4a2713aSLionel Sambuc 141f4a2713aSLionel Sambuc template<typename T> 142f4a2713aSLionel Sambuc T pi0 = T(3.1415926535897932385); 143f4a2713aSLionel Sambuc template int pi0<int>; // expected-note {{previous explicit instantiation is here}} 144f4a2713aSLionel Sambuc template int pi0<int>; // expected-error {{duplicate explicit instantiation of 'pi0<int>'}} 145f4a2713aSLionel Sambuc 146f4a2713aSLionel Sambuc template<typename T> 147f4a2713aSLionel Sambuc CONST T pi1a = T(3.1415926535897932385); // expected-note {{variable template 'pi1a' declared here}} 148f4a2713aSLionel Sambuc template int pi1a<int>; // expected-error {{type 'int' of explicit instantiation of 'pi1a' does not match expected type 'const int'}} 149f4a2713aSLionel Sambuc 150f4a2713aSLionel Sambuc template<typename T> 151f4a2713aSLionel Sambuc CONST T pi1b = T(3.1415926535897932385); // expected-note {{variable template 'pi1b' declared here}} 152f4a2713aSLionel Sambuc template int pi1b<const int>; // expected-error {{type 'int' of explicit instantiation of 'pi1b' does not match expected type 'const const int'}} 153f4a2713aSLionel Sambuc 154f4a2713aSLionel Sambuc template<typename T> 155f4a2713aSLionel Sambuc CONST T pi1 = T(3.1415926535897932385); 156f4a2713aSLionel Sambuc template CONST int pi1<int>; // expected-note {{previous explicit instantiation is here}} 157f4a2713aSLionel Sambuc template CONST int pi1<int>; // expected-error {{duplicate explicit instantiation of 'pi1<int>'}} 158f4a2713aSLionel Sambuc 159f4a2713aSLionel Sambuc #ifndef PRECXX11 160f4a2713aSLionel Sambuc namespace auto_var { 161f4a2713aSLionel Sambuc template<typename T> auto var0 = T(); 162f4a2713aSLionel Sambuc template auto var0<int>; // expected-error {{'auto' variable template instantiation is not allowed}} 163f4a2713aSLionel Sambuc 164f4a2713aSLionel Sambuc template<typename T> auto var = T(); 165f4a2713aSLionel Sambuc template int var<int>; 166f4a2713aSLionel Sambuc } 167f4a2713aSLionel Sambuc #endif 168f4a2713aSLionel Sambuc 169f4a2713aSLionel Sambuc template<typename=int> int missing_args; // expected-note {{here}} 170f4a2713aSLionel Sambuc template int missing_args; // expected-error {{must specify a template argument list}} 171f4a2713aSLionel Sambuc 172f4a2713aSLionel Sambuc namespace extern_var { 173f4a2713aSLionel Sambuc // TODO: 174f4a2713aSLionel Sambuc } 175f4a2713aSLionel Sambuc } 176f4a2713aSLionel Sambuc 177f4a2713aSLionel Sambuc namespace explicit_specialization { 178f4a2713aSLionel Sambuc 179f4a2713aSLionel Sambuc namespace good { 180f4a2713aSLionel Sambuc template<typename T1, typename T2> 181f4a2713aSLionel Sambuc CONST int pi2 = 1; 182f4a2713aSLionel Sambuc 183f4a2713aSLionel Sambuc template<typename T> 184f4a2713aSLionel Sambuc CONST int pi2<T,int> = 2; 185f4a2713aSLionel Sambuc 186f4a2713aSLionel Sambuc template<typename T> 187f4a2713aSLionel Sambuc CONST int pi2<int,T> = 3; 188f4a2713aSLionel Sambuc 189f4a2713aSLionel Sambuc template<> CONST int pi2<int,int> = 4; 190f4a2713aSLionel Sambuc 191f4a2713aSLionel Sambuc #ifndef PRECXX11 foo()192f4a2713aSLionel Sambuc void foo() { 193f4a2713aSLionel Sambuc static_assert(pi2<int,int> == 4, ""); 194f4a2713aSLionel Sambuc static_assert(pi2<float,int> == 2, ""); 195f4a2713aSLionel Sambuc static_assert(pi2<int,float> == 3, ""); 196f4a2713aSLionel Sambuc static_assert(pi2<int,float> == pi2<int,double>, ""); 197f4a2713aSLionel Sambuc static_assert(pi2<float,float> == 1, ""); 198f4a2713aSLionel Sambuc static_assert(pi2<float,float> == pi2<float,double>, ""); 199f4a2713aSLionel Sambuc } 200f4a2713aSLionel Sambuc #endif 201f4a2713aSLionel Sambuc } 202f4a2713aSLionel Sambuc 203f4a2713aSLionel Sambuc namespace ambiguous { 204f4a2713aSLionel Sambuc 205f4a2713aSLionel Sambuc template<typename T1, typename T2> 206f4a2713aSLionel Sambuc CONST int pi2 = 1; 207f4a2713aSLionel Sambuc 208f4a2713aSLionel Sambuc template<typename T> 209f4a2713aSLionel Sambuc CONST int pi2<T,int> = 2; // expected-note {{partial specialization matches [with T = int]}} 210f4a2713aSLionel Sambuc 211f4a2713aSLionel Sambuc template<typename T> 212f4a2713aSLionel Sambuc CONST int pi2<int,T> = 3; // expected-note {{partial specialization matches [with T = int]}} 213f4a2713aSLionel Sambuc foo()214f4a2713aSLionel Sambuc void foo() { 215f4a2713aSLionel Sambuc int a = pi2<int,int>; // expected-error {{ambiguous partial specializations of 'pi2<int, int>'}} 216f4a2713aSLionel Sambuc } 217f4a2713aSLionel Sambuc } 218f4a2713aSLionel Sambuc 219f4a2713aSLionel Sambuc namespace type_changes { 220f4a2713aSLionel Sambuc 221f4a2713aSLionel Sambuc template<typename T> 222f4a2713aSLionel Sambuc T pi0 = T(3.1415926535897932385); 223f4a2713aSLionel Sambuc 224f4a2713aSLionel Sambuc template<> float pi0<int> = 10; 225f4a2713aSLionel Sambuc template<> int pi0<const int> = 10; 226f4a2713aSLionel Sambuc 227f4a2713aSLionel Sambuc template<typename T> 228f4a2713aSLionel Sambuc T pi1 = T(3.1415926535897932385); 229f4a2713aSLionel Sambuc template<> CONST int pi1<int> = 10; 230f4a2713aSLionel Sambuc 231f4a2713aSLionel Sambuc template<typename T> 232f4a2713aSLionel Sambuc T pi2 = T(3.1415926535897932385); 233f4a2713aSLionel Sambuc template<> int pi2<const int> = 10; 234f4a2713aSLionel Sambuc 235f4a2713aSLionel Sambuc template<typename T> 236f4a2713aSLionel Sambuc CONST T pi4 = T(3.1415926535897932385); 237f4a2713aSLionel Sambuc template<> int pi4<int> = 10; 238f4a2713aSLionel Sambuc } 239f4a2713aSLionel Sambuc 240f4a2713aSLionel Sambuc namespace redefinition { 241f4a2713aSLionel Sambuc template<typename T> 242f4a2713aSLionel Sambuc T pi0 = T(3.1415926535897932385); 243f4a2713aSLionel Sambuc 244f4a2713aSLionel Sambuc template<> int pi0<int> = 10; // expected-note 3{{previous definition is here}} 245f4a2713aSLionel Sambuc #ifndef PRECXX11 246f4a2713aSLionel Sambuc // expected-note@-2 {{previous definition is here}} 247f4a2713aSLionel Sambuc #endif 248f4a2713aSLionel Sambuc template<> int pi0<int> = 10; // expected-error {{redefinition of 'pi0<int>'}} 249f4a2713aSLionel Sambuc template<> CONST int pi0<int> = 10; // expected-error {{redefinition of 'pi0' with a different type: 'const int' vs 'int'}} 250f4a2713aSLionel Sambuc template<> float pi0<int> = 10; // expected-error {{redefinition of 'pi0' with a different type: 'float' vs 'int'}} 251f4a2713aSLionel Sambuc #ifndef PRECXX11 252f4a2713aSLionel Sambuc template<> auto pi0<int> = 10; // expected-error {{redefinition of 'pi0<int>'}} 253f4a2713aSLionel Sambuc #endif 254f4a2713aSLionel Sambuc 255f4a2713aSLionel Sambuc 256f4a2713aSLionel Sambuc template<typename T> 257f4a2713aSLionel Sambuc CONST T pi1 = T(3.1415926535897932385); 258f4a2713aSLionel Sambuc 259f4a2713aSLionel Sambuc template<> CONST int pi1<int> = 10; // expected-note {{previous definition is here}} 260f4a2713aSLionel Sambuc template<> CONST int pi1<int> = 10; // expected-error {{redefinition of 'pi1<int>'}} 261f4a2713aSLionel Sambuc } 262f4a2713aSLionel Sambuc 263f4a2713aSLionel Sambuc namespace before_instantiation { 264f4a2713aSLionel Sambuc template<typename T> 265f4a2713aSLionel Sambuc T pi0 = T(3.1415926535897932385); // expected-note {{variable template 'pi0' declared here}} 266f4a2713aSLionel Sambuc 267f4a2713aSLionel Sambuc template<> int pi0<int> = 10; 268f4a2713aSLionel Sambuc template int pi0<int>; 269f4a2713aSLionel Sambuc template float pi0<int>; // expected-error {{type 'float' of explicit instantiation of 'pi0' does not match expected type}} 270f4a2713aSLionel Sambuc 271f4a2713aSLionel Sambuc template<typename T1, typename T2> 272f4a2713aSLionel Sambuc CONST int pi2 = 1; 273f4a2713aSLionel Sambuc 274f4a2713aSLionel Sambuc template<typename T> CONST int pi2<T,int> = 2; 275f4a2713aSLionel Sambuc template CONST int pi2<int,int>; 276f4a2713aSLionel Sambuc } 277f4a2713aSLionel Sambuc namespace after_instantiation { 278f4a2713aSLionel Sambuc template<typename T> 279f4a2713aSLionel Sambuc T pi0 = T(3.1415926535897932385); 280f4a2713aSLionel Sambuc 281f4a2713aSLionel Sambuc template int pi0<int>; // expected-note 2{{explicit instantiation first required here}} 282f4a2713aSLionel Sambuc template<> int pi0<int> = 10; // expected-error {{explicit specialization of 'pi0' after instantiation}} 283f4a2713aSLionel Sambuc template<> float pi0<int>; // expected-error {{explicit specialization of 'pi0' after instantiation}} 284f4a2713aSLionel Sambuc 285f4a2713aSLionel Sambuc template<typename T1, typename T2> 286f4a2713aSLionel Sambuc CONST int pi2 = 1; 287f4a2713aSLionel Sambuc 288f4a2713aSLionel Sambuc template CONST int pi2<int,int>; 289f4a2713aSLionel Sambuc template<typename T> CONST int pi2<T,int> = 2; 290f4a2713aSLionel Sambuc } 291f4a2713aSLionel Sambuc 292f4a2713aSLionel Sambuc #ifndef PRECXX11 293f4a2713aSLionel Sambuc namespace auto_var { 294f4a2713aSLionel Sambuc template<typename T, typename> auto var0 = T(); 295f4a2713aSLionel Sambuc template<typename T> auto var0<T,int> = T(); 296f4a2713aSLionel Sambuc template<> auto var0<int,int> = 7; 297f4a2713aSLionel Sambuc 298f4a2713aSLionel Sambuc template<typename T, typename> auto var = T(); 299f4a2713aSLionel Sambuc template<typename T> T var<T,int> = T(5); 300f4a2713aSLionel Sambuc template<> int var<int,int> = 7; 301f4a2713aSLionel Sambuc foo()302f4a2713aSLionel Sambuc void foo() { 303f4a2713aSLionel Sambuc int i0 = var0<int,int>; 304f4a2713aSLionel Sambuc int b = var<int,int>; 305f4a2713aSLionel Sambuc } 306f4a2713aSLionel Sambuc } 307f4a2713aSLionel Sambuc #endif 308f4a2713aSLionel Sambuc 309f4a2713aSLionel Sambuc namespace extern_var { 310f4a2713aSLionel Sambuc // TODO: 311f4a2713aSLionel Sambuc } 312f4a2713aSLionel Sambuc 313f4a2713aSLionel Sambuc namespace diff_type { 314f4a2713aSLionel Sambuc // TODO: 315f4a2713aSLionel Sambuc template<typename T> T* var = new T(); 316f4a2713aSLionel Sambuc #ifndef PRECXX11 317f4a2713aSLionel Sambuc template<typename T> auto var<T*> = T(); // expected-note {{previous definition is here}} 318f4a2713aSLionel Sambuc template<typename T> T var<T*> = T(); // expected-error {{redefinition of 'var' with a different type: 'T' vs 'auto'}} 319f4a2713aSLionel Sambuc #endif 320f4a2713aSLionel Sambuc } 321f4a2713aSLionel Sambuc } 322f4a2713aSLionel Sambuc 323f4a2713aSLionel Sambuc namespace narrowing { 324f4a2713aSLionel Sambuc template<typename T> T v = {1234}; // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1234 to}} 325f4a2713aSLionel Sambuc #ifndef PRECXX11 326f4a2713aSLionel Sambuc // expected-error@-2 {{constant expression evaluates to 1234 which cannot be narrowed to type 'char'}}\ 327*0a6a1f1dSLionel Sambuc // expected-note@-2 {{insert an explicit cast to silence this issue}} 328f4a2713aSLionel Sambuc #endif 329f4a2713aSLionel Sambuc int k = v<char>; // expected-note {{in instantiation of variable template specialization 'narrowing::v<char>' requested here}} 330f4a2713aSLionel Sambuc } 331f4a2713aSLionel Sambuc 332f4a2713aSLionel Sambuc namespace use_in_structs { 333f4a2713aSLionel Sambuc // TODO: 334f4a2713aSLionel Sambuc } 335f4a2713aSLionel Sambuc 336f4a2713aSLionel Sambuc namespace attributes { 337f4a2713aSLionel Sambuc // TODO: 338f4a2713aSLionel Sambuc } 339f4a2713aSLionel Sambuc 340f4a2713aSLionel Sambuc #ifndef PRECXX11 341f4a2713aSLionel Sambuc namespace arrays { 342f4a2713aSLionel Sambuc template<typename T> 343f4a2713aSLionel Sambuc T* arr = new T[10]{T(10), T(23)}; 344f4a2713aSLionel Sambuc 345f4a2713aSLionel Sambuc float f = 10.5; 346f4a2713aSLionel Sambuc template<> float* arr<float> = &f; 347f4a2713aSLionel Sambuc bar()348f4a2713aSLionel Sambuc void bar() { 349f4a2713aSLionel Sambuc int *iarr = arr<int>; 350f4a2713aSLionel Sambuc iarr[0] = 1; 351f4a2713aSLionel Sambuc iarr[2] = 3; 352f4a2713aSLionel Sambuc iarr[6] = -2; 353f4a2713aSLionel Sambuc 354f4a2713aSLionel Sambuc float ff = *arr<float>; 355f4a2713aSLionel Sambuc float nof = arr<float>[3]; // No bounds-check in C++ 356f4a2713aSLionel Sambuc } 357f4a2713aSLionel Sambuc } 358f4a2713aSLionel Sambuc #endif 359f4a2713aSLionel Sambuc 360f4a2713aSLionel Sambuc namespace nested { 361f4a2713aSLionel Sambuc 362f4a2713aSLionel Sambuc namespace n0a { 363f4a2713aSLionel Sambuc template<typename T> 364f4a2713aSLionel Sambuc T pi0a = T(3.1415926535897932385); 365f4a2713aSLionel Sambuc } 366f4a2713aSLionel Sambuc 367f4a2713aSLionel Sambuc using namespace n0a; 368f4a2713aSLionel Sambuc int i0a = pi0a<int>; 369f4a2713aSLionel Sambuc 370f4a2713aSLionel Sambuc template float pi0a<float>; 371f4a2713aSLionel Sambuc float f0a = pi0a<float>; 372f4a2713aSLionel Sambuc 373f4a2713aSLionel Sambuc template<> double pi0a<double> = 5.2; 374f4a2713aSLionel Sambuc double d0a = pi0a<double>; 375f4a2713aSLionel Sambuc 376f4a2713aSLionel Sambuc namespace n0b { 377f4a2713aSLionel Sambuc template<typename T> 378f4a2713aSLionel Sambuc T pi0b = T(3.1415926535897932385); 379f4a2713aSLionel Sambuc } 380f4a2713aSLionel Sambuc 381f4a2713aSLionel Sambuc int i0b = n0b::pi0b<int>; 382f4a2713aSLionel Sambuc 383f4a2713aSLionel Sambuc template float n0b::pi0b<float>; 384f4a2713aSLionel Sambuc float f0b = n0b::pi0b<float>; 385f4a2713aSLionel Sambuc 386f4a2713aSLionel Sambuc template<> double n0b::pi0b<double> = 5.2; 387f4a2713aSLionel Sambuc double d0b = n0b::pi0b<double>; 388f4a2713aSLionel Sambuc 389f4a2713aSLionel Sambuc namespace n1 { 390f4a2713aSLionel Sambuc template<typename T> 391*0a6a1f1dSLionel Sambuc T pi1a = T(3.1415926535897932385); // expected-note {{explicitly specialized declaration is here}} 392f4a2713aSLionel Sambuc #ifndef PRECXX11 393f4a2713aSLionel Sambuc // expected-note@-2 {{explicit instantiation refers here}} 394f4a2713aSLionel Sambuc #endif 395f4a2713aSLionel Sambuc 396f4a2713aSLionel Sambuc template<typename T> 397f4a2713aSLionel Sambuc T pi1b = T(3.1415926535897932385); // expected-note {{explicitly specialized declaration is here}} 398f4a2713aSLionel Sambuc #ifndef PRECXX11 399f4a2713aSLionel Sambuc // expected-note@-2 {{explicit instantiation refers here}} 400f4a2713aSLionel Sambuc #endif 401f4a2713aSLionel Sambuc } 402f4a2713aSLionel Sambuc 403f4a2713aSLionel Sambuc namespace use_n1a { 404f4a2713aSLionel Sambuc using namespace n1; 405f4a2713aSLionel Sambuc int i1 = pi1a<int>; 406f4a2713aSLionel Sambuc 407f4a2713aSLionel Sambuc template float pi1a<float>; 408f4a2713aSLionel Sambuc #ifndef PRECXX11 409f4a2713aSLionel Sambuc // expected-error@-2 {{explicit instantiation of 'pi1a<float>' not in a namespace enclosing 'n1'}} 410f4a2713aSLionel Sambuc #endif 411f4a2713aSLionel Sambuc float f1 = pi1a<float>; 412f4a2713aSLionel Sambuc 413*0a6a1f1dSLionel Sambuc template<> double pi1a<double> = 5.2; // expected-error {{variable template specialization of 'pi1a' must originally be declared in namespace 'n1'}} 414f4a2713aSLionel Sambuc double d1 = pi1a<double>; 415f4a2713aSLionel Sambuc } 416f4a2713aSLionel Sambuc 417f4a2713aSLionel Sambuc namespace use_n1b { 418f4a2713aSLionel Sambuc int i1 = n1::pi1b<int>; 419f4a2713aSLionel Sambuc 420f4a2713aSLionel Sambuc template float n1::pi1b<float>; 421f4a2713aSLionel Sambuc #ifndef PRECXX11 422f4a2713aSLionel Sambuc // expected-error@-2 {{explicit instantiation of 'pi1b<float>' not in a namespace enclosing 'n1'}} 423f4a2713aSLionel Sambuc #endif 424f4a2713aSLionel Sambuc float f1 = n1::pi1b<float>; 425f4a2713aSLionel Sambuc 426f4a2713aSLionel Sambuc template<> double n1::pi1b<double> = 5.2; // expected-error {{cannot define or redeclare 'pi1b' here because namespace 'use_n1b' does not enclose namespace 'n1'}} \ 427f4a2713aSLionel Sambuc // expected-error {{variable template specialization of 'pi1b' must originally be declared in namespace 'n1'}} 428f4a2713aSLionel Sambuc double d1 = n1::pi1b<double>; 429f4a2713aSLionel Sambuc } 430f4a2713aSLionel Sambuc } 431f4a2713aSLionel Sambuc 432*0a6a1f1dSLionel Sambuc namespace nested_name { 433*0a6a1f1dSLionel Sambuc template<typename T> int a; // expected-note {{variable template 'a' declared here}} 434*0a6a1f1dSLionel Sambuc a<int>::b c; // expected-error {{qualified name refers into a specialization of variable template 'a'}} 435*0a6a1f1dSLionel Sambuc 436*0a6a1f1dSLionel Sambuc class a<int> {}; // expected-error {{identifier followed by '<' indicates a class template specialization but 'a' refers to a variable template}} 437*0a6a1f1dSLionel Sambuc enum a<int> {}; // expected-error {{expected identifier or '{'}} expected-warning {{does not declare anything}} 438*0a6a1f1dSLionel Sambuc } 439*0a6a1f1dSLionel Sambuc 440*0a6a1f1dSLionel Sambuc namespace PR18530 { 441*0a6a1f1dSLionel Sambuc template<typename T> int a; 442*0a6a1f1dSLionel Sambuc int a<int>; // expected-error {{requires 'template<>'}} 443*0a6a1f1dSLionel Sambuc } 444*0a6a1f1dSLionel Sambuc 445*0a6a1f1dSLionel Sambuc namespace PR19152 { 446*0a6a1f1dSLionel Sambuc #ifndef PRECXX11 447*0a6a1f1dSLionel Sambuc template<typename T> const auto x = 1; 448*0a6a1f1dSLionel Sambuc static_assert(x<int> == 1, ""); 449*0a6a1f1dSLionel Sambuc #endif 450*0a6a1f1dSLionel Sambuc } 451*0a6a1f1dSLionel Sambuc 452*0a6a1f1dSLionel Sambuc namespace PR19169 { 453*0a6a1f1dSLionel Sambuc template <typename T> int* f(); 454*0a6a1f1dSLionel Sambuc template <typename T> void f(); 455*0a6a1f1dSLionel Sambuc template<> int f<double>; // expected-error {{no variable template matches specialization; did you mean to use 'f' as function template instead?}} 456*0a6a1f1dSLionel Sambuc 457*0a6a1f1dSLionel Sambuc template <typename T> void g(); 458*0a6a1f1dSLionel Sambuc template<> int g<double>; // expected-error {{no variable template matches specialization; did you mean to use 'g' as function template instead?}} 459*0a6a1f1dSLionel Sambuc } 460*0a6a1f1dSLionel Sambuc 461