p5.cpp revision beb9e4f17fcaca29313fc7a56de977c5dd449c46
1// RUN: %clang_cc1 -fsyntax-only -verify %s 2 3// C++0x [temp.arg.nontype] p5: 4// The following conversions are performed on each expression used as 5// a non-type template-argument. If a non-type template-argument cannot be 6// converted to the type of the corresponding template-parameter then the 7// program is ill-formed. 8// -- for a non-type template-parameter of integral or enumeration type, 9// integral promotions (4.5) and integral conversions (4.7) are applied. 10// -- for a non-type template-parameter of type pointer to object, 11// qualification conversions (4.4) and the array-to-pointer conversion 12// (4.2) are applied; if the template-argument is of type 13// std::nullptr_t, the null pointer conversion (4.10) is applied. 14namespace pointer_to_object_parameters { 15 // PR6226 16 struct Str { 17 Str(const char *); 18 }; 19 20 template<const char *s> 21 struct A { 22 Str get() { return s; } 23 }; 24 25 char hello[6] = "Hello"; 26 extern const char world[6]; 27 const char world[6] = "world"; 28 void test() { 29 (void)A<hello>().get(); 30 (void)A<world>().get(); 31 } 32 33 class X { 34 public: 35 X(); 36 X(int, int); 37 operator int() const; 38 }; 39 40 template<X const *Ptr> struct A2; 41 42 X *X_ptr; 43 X an_X; 44 X array_of_Xs[10]; 45 A2<X_ptr> *a12; 46 A2<array_of_Xs> *a13; 47 A2<&an_X> *a13_2; 48 A2<(&an_X)> *a13_3; // expected-error{{non-type template argument cannot be surrounded by parentheses}} 49} 50 51// -- For a non-type template-parameter of type reference to object, no 52// conversions apply. The type referred to by the reference may be more 53// cv-qualified than the (otherwise identical) type of the 54// template-argument. The template-parameter is bound directly to the 55// template-argument, which shall be an lvalue. 56namespace reference_parameters { 57 template <int& N> struct S0 { }; // expected-note 3 {{template parameter is declared here}} 58 template <const int& N> struct S1 { }; // expected-note 2 {{template parameter is declared here}} 59 template <volatile int& N> struct S2 { }; // expected-note 2 {{template parameter is declared here}} 60 template <const volatile int& N> struct S3 { }; 61 int i; 62 extern const int ci; 63 volatile int vi; 64 extern const volatile int cvi; 65 void test() { 66 S0<i> s0; 67 S0<ci> s0c; // expected-error{{reference binding of non-type template parameter of type 'int &' to template argument of type 'int const' ignores qualifiers}} 68 S0<vi> s0v; // expected-error{{reference binding of non-type template parameter of type 'int &' to template argument of type 'int volatile' ignores qualifiers}} 69 S0<cvi> s0cv; // expected-error{{reference binding of non-type template parameter of type 'int &' to template argument of type 'int const volatile' ignores qualifiers}} 70 71 S1<i> s1; 72 S1<ci> s1c; 73 S1<vi> s1v; // expected-error{{reference binding of non-type template parameter of type 'int const &' to template argument of type 'int volatile' ignores qualifiers}} 74 S1<cvi> s1cv; // expected-error{{reference binding of non-type template parameter of type 'int const &' to template argument of type 'int const volatile' ignores qualifiers}} 75 76 S2<i> s2; 77 S2<ci> s2c; // expected-error{{reference binding of non-type template parameter of type 'int volatile &' to template argument of type 'int const' ignores qualifiers}} 78 S2<vi> s2v; 79 S2<cvi> s2cv; // expected-error{{reference binding of non-type template parameter of type 'int volatile &' to template argument of type 'int const volatile' ignores qualifiers}} 80 81 S3<i> s3; 82 S3<ci> s3c; 83 S3<vi> s3v; 84 S3<cvi> s3cv; 85 } 86} 87 88// -- For a non-type template-parameter of type pointer to function, the 89// function-to-pointer conversion (4.3) is applied; if the 90// template-argument is of type std::nullptr_t, the null pointer 91// conversion (4.10) is applied. If the template-argument represents 92// a set of overloaded functions (or a pointer to such), the matching 93// function is selected from the set (13.4). 94// -- For a non-type template-parameter of type reference to function, no 95// conversions apply. If the template-argument represents a set of 96// overloaded functions, the matching function is selected from the set 97// (13.4). 98// -- For a non-type template-parameter of type pointer to member function, 99// if the template-argument is of type std::nullptr_t, the null member 100// pointer conversion (4.11) is applied; otherwise, no conversions 101// apply. If the template-argument represents a set of overloaded member 102// functions, the matching member function is selected from the set 103// (13.4). 104// -- For a non-type template-parameter of type pointer to data member, 105// qualification conversions (4.4) are applied; if the template-argument 106// is of type std::nullptr_t, the null member pointer conversion (4.11) 107// is applied. 108