Home | History | Annotate | Download | only in temp.arg.nontype
      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 namespace integral_parameters {
     11   template<short s> struct X0 { };
     12   X0<17> x0i;
     13   X0<'a'> x0c;
     14   template<char c> struct X1 { };
     15   X1<100l> x1l;
     16 }
     17 
     18 //     -- for a non-type template-parameter of type pointer to object,
     19 //        qualification conversions (4.4) and the array-to-pointer conversion
     20 //        (4.2) are applied; if the template-argument is of type
     21 //        std::nullptr_t, the null pointer conversion (4.10) is applied.
     22 namespace pointer_to_object_parameters {
     23   // PR6226
     24   struct Str {
     25     Str(const char *);
     26   };
     27 
     28   template<const char *s>
     29   struct A {
     30     Str get() { return s; }
     31   };
     32 
     33   char hello[6] = "Hello";
     34   extern const char world[6];
     35   const char world[6] = "world";
     36   void test() {
     37     (void)A<hello>().get();
     38     (void)A<world>().get();
     39   }
     40 
     41   class X {
     42   public:
     43     X();
     44     X(int, int);
     45     operator int() const;
     46   };
     47 
     48   template<X const *Ptr> struct A2; // expected-note{{template parameter is declared here}}
     49 
     50   X *X_ptr;
     51   X an_X;
     52   X array_of_Xs[10];
     53   A2<X_ptr> *a12; // expected-error{{must have its address taken}}
     54   A2<array_of_Xs> *a13;
     55   A2<&an_X> *a13_2;
     56   A2<(&an_X)> *a13_3; // expected-warning{{address non-type template argument cannot be surrounded by parentheses}}
     57 
     58   // PR6244
     59   struct X1 {} X1v;
     60   template <X1*> struct X2 { };
     61   template <X1* Value> struct X3 : X2<Value> { };
     62   struct X4 : X3<&X1v> { };
     63 
     64   // PR6563
     65   int *bar;
     66   template <int *> struct zed {}; // expected-note 2{{template parameter is declared here}}
     67   void g(zed<bar>*); // expected-error{{must have its address taken}}
     68 
     69   int baz;
     70   void g2(zed<baz>*); // expected-error{{must have its address taken}}
     71 
     72   void g3(zed<&baz>*); // okay
     73 }
     74 
     75 //     -- For a non-type template-parameter of type reference to object, no
     76 //        conversions apply. The type referred to by the reference may be more
     77 //        cv-qualified than the (otherwise identical) type of the
     78 //        template-argument. The template-parameter is bound directly to the
     79 //        template-argument, which shall be an lvalue.
     80 namespace reference_parameters {
     81   template <int& N> struct S0 { }; // expected-note 3 {{template parameter is declared here}}
     82   template <const int& N> struct S1 { }; // expected-note 2 {{template parameter is declared here}}
     83   template <volatile int& N> struct S2 { }; // expected-note 2 {{template parameter is declared here}}
     84   template <const volatile int& N> struct S3 { };
     85   int i;
     86   extern const int ci;
     87   volatile int vi;
     88   extern const volatile int cvi;
     89   void test() {
     90     S0<i> s0;
     91     S0<ci> s0c; // expected-error{{reference binding of non-type template parameter of type 'int &' to template argument of type 'const int' ignores qualifiers}}
     92     S0<vi> s0v; // expected-error{{reference binding of non-type template parameter of type 'int &' to template argument of type 'volatile int' ignores qualifiers}}
     93     S0<cvi> s0cv; // expected-error{{reference binding of non-type template parameter of type 'int &' to template argument of type 'const volatile int' ignores qualifiers}}
     94 
     95     S1<i> s1;
     96     S1<ci> s1c;
     97     S1<vi> s1v; // expected-error{{reference binding of non-type template parameter of type 'const int &' to template argument of type 'volatile int' ignores qualifiers}}
     98     S1<cvi> s1cv; // expected-error{{reference binding of non-type template parameter of type 'const int &' to template argument of type 'const volatile int' ignores qualifiers}}
     99 
    100     S2<i> s2;
    101     S2<ci> s2c; // expected-error{{reference binding of non-type template parameter of type 'volatile int &' to template argument of type 'const int' ignores qualifiers}}
    102     S2<vi> s2v;
    103     S2<cvi> s2cv; // expected-error{{reference binding of non-type template parameter of type 'volatile int &' to template argument of type 'const volatile int' ignores qualifiers}}
    104 
    105     S3<i> s3;
    106     S3<ci> s3c;
    107     S3<vi> s3v;
    108     S3<cvi> s3cv;
    109   }
    110 
    111   namespace PR6250 {
    112     template <typename T, const T &ref> void inc() {
    113       ref++; // expected-error{{read-only variable is not assignable}}
    114     }
    115 
    116     template<typename T, const T &ref> void bind() {
    117       T &ref2 = ref; // expected-error{{drops qualifiers}}
    118     }
    119 
    120     int counter;
    121     void test() {
    122       inc<int, counter>(); // expected-note{{instantiation of}}
    123       bind<int, counter>(); // expected-note{{instantiation of}}
    124     }
    125   }
    126 
    127   namespace PR6749 {
    128     template <int& i> struct foo {}; // expected-note{{template parameter is declared here}}
    129     int x, &y = x;
    130     foo<y> f; // expected-error{{is not an object}}
    131   }
    132 }
    133 
    134 //     -- For a non-type template-parameter of type pointer to function, the
    135 //        function-to-pointer conversion (4.3) is applied; if the
    136 //        template-argument is of type std::nullptr_t, the null pointer
    137 //        conversion (4.10) is applied. If the template-argument represents
    138 //        a set of overloaded functions (or a pointer to such), the matching
    139 //        function is selected from the set (13.4).
    140 namespace pointer_to_function {
    141   template<int (*)(int)> struct X0 { }; // expected-note 3{{template parameter is declared here}}
    142   int f(int);
    143   int f(float);
    144   int g(float);
    145   int (*funcptr)(int);
    146   void x0a(X0<f>);
    147   void x0b(X0<&f>);
    148   void x0c(X0<g>); // expected-error{{non-type template argument of type 'int (float)' cannot be converted to a value of type 'int (*)(int)'}}
    149   void x0d(X0<&g>); // expected-error{{non-type template argument of type 'int (*)(float)' cannot be converted to a value of type 'int (*)(int)'}}
    150   void x0e(X0<funcptr>); // expected-error{{must have its address taken}}
    151 }
    152 
    153 //     -- For a non-type template-parameter of type reference to function, no
    154 //        conversions apply. If the template-argument represents a set of
    155 //        overloaded functions, the matching function is selected from the set
    156 //        (13.4).
    157 namespace reference_to_function {
    158   template<int (&)(int)> struct X0 { }; // expected-note 4{{template parameter is declared here}}
    159   int f(int);
    160   int f(float);
    161   int g(float);
    162   int (*funcptr)(int);
    163   void x0a(X0<f>);
    164   void x0b(X0<&f>); // expected-error{{address taken in non-type template argument for template parameter of reference type 'int (&)(int)'}}
    165   void x0c(X0<g>); // expected-error{{non-type template parameter of reference type 'int (&)(int)' cannot bind to template argument of type 'int (float)'}}
    166   void x0d(X0<&g>); // expected-error{{address taken in non-type template argument for template parameter of reference type 'int (&)(int)'}}
    167   void x0e(X0<funcptr>); // expected-error{{non-type template parameter of reference type 'int (&)(int)' cannot bind to template argument of type 'int (*)(int)'}}
    168 }
    169 //     -- For a non-type template-parameter of type pointer to member function,
    170 //        if the template-argument is of type std::nullptr_t, the null member
    171 //        pointer conversion (4.11) is applied; otherwise, no conversions
    172 //        apply. If the template-argument represents a set of overloaded member
    173 //        functions, the matching member function is selected from the set
    174 //        (13.4).
    175 namespace pointer_to_member_function {
    176   struct X { };
    177   struct Y : X {
    178     int f(int);
    179     int g(int);
    180     int g(float);
    181     float h(float);
    182   };
    183 
    184   template<int (Y::*)(int)> struct X0 {}; // expected-note{{template parameter is declared here}}
    185   X0<&Y::f> x0a;
    186   X0<&Y::g> x0b;
    187   X0<&Y::h> x0c; // expected-error{{non-type template argument of type 'float (pointer_to_member_function::Y::*)(float)' cannot be converted to a value of type 'int (pointer_to_member_function::Y::*)(int)'}}
    188 }
    189 
    190 //     -- For a non-type template-parameter of type pointer to data member,
    191 //        qualification conversions (4.4) are applied; if the template-argument
    192 //        is of type std::nullptr_t, the null member pointer conversion (4.11)
    193 //        is applied.
    194 namespace pointer_to_member_data {
    195   struct X { int x; };
    196   struct Y : X { int y; };
    197 
    198   template<int Y::*> struct X0 {}; // expected-note{{template parameter is declared here}}
    199   X0<&Y::y> x0a;
    200   X0<&Y::x> x0b;  // expected-error{{non-type template argument of type 'int pointer_to_member_data::X::*' cannot be converted to a value of type 'int pointer_to_member_data::Y::*'}}
    201 
    202   // Test qualification conversions
    203   template<const int Y::*> struct X1 {};
    204   X1<&Y::y> x1a;
    205 }
    206