• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -fsyntax-only -verify -triple=x86_64-linux-gnu %s
2 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -triple=x86_64-linux-gnu %s -DCPP11ONLY
3 
4 // C++11 [temp.arg.nontype]p1:
5 //
6 //   A template-argument for a non-type, non-template template-parameter shall
7 //   be one of:
8 //   -- an integral constant expression; or
9 //   -- the name of a non-type template-parameter ; or
10 #ifndef CPP11ONLY
11 
12 namespace non_type_tmpl_param {
13   template <int N> struct X0 { X0(); };
X0()14   template <int N> X0<N>::X0() { }
15   template <int* N> struct X1 { X1(); };
X1()16   template <int* N> X1<N>::X1() { }
17   template <int& N> struct X3 { X3(); };
X3()18   template <int& N> X3<N>::X3() { }
19   template <int (*F)(int)> struct X4 { X4(); };
X4()20   template <int (*F)(int)> X4<F>::X4() { }
21   template <typename T, int (T::* M)(int)> struct X5 { X5(); };
X5()22   template <typename T, int (T::* M)(int)> X5<T, M>::X5() { }
23 }
24 
25 //   -- a constant expression that designates the address of an object with
26 //      static storage duration and external or internal linkage or a function
27 //      with external or internal linkage, including function templates and
28 //      function template-ids, but excluting non-static class members, expressed
29 //      (ignoring parentheses) as & id-expression, except that the & may be
30 //      omitted if the name refers to a function or array and shall be omitted
31 //      if the corresopnding template-parameter is a reference; or
32 namespace addr_of_obj_or_func {
33   template <int* p> struct X0 { }; // expected-note 5{{here}}
34   template <int (*fp)(int)> struct X1 { };
35   template <int &p> struct X2 { }; // expected-note 4{{here}}
36   template <const int &p> struct X2k { }; // expected-note {{here}}
37   template <int (&fp)(int)> struct X3 { }; // expected-note 4{{here}}
38 
39   int i = 42;
40   int iarr[10];
41   int f(int i);
42   const int ki = 9; // expected-note 5{{here}}
43   __thread int ti = 100; // expected-note 2{{here}}
44   static int f_internal(int); // expected-note 4{{here}}
45   template <typename T> T f_tmpl(T t);
46   struct S { union { int NonStaticMember; }; };
47 
test()48   void test() {
49     X0<i> x0a; // expected-error {{must have its address taken}}
50     X0<&i> x0a_addr;
51     X0<iarr> x0b;
52     X0<&iarr> x0b_addr; // expected-error {{cannot be converted to a value of type 'int *'}}
53     X0<ki> x0c; // expected-error {{must have its address taken}} expected-warning {{internal linkage is a C++11 extension}}
54     X0<&ki> x0c_addr; // expected-error {{cannot be converted to a value of type 'int *'}} expected-warning {{internal linkage is a C++11 extension}}
55     X0<&ti> x0d_addr; // expected-error {{refers to thread-local object}}
56     X1<f> x1a;
57     X1<&f> x1a_addr;
58     X1<f_tmpl> x1b;
59     X1<&f_tmpl> x1b_addr;
60     X1<f_tmpl<int> > x1c;
61     X1<&f_tmpl<int> > x1c_addr;
62     X1<f_internal> x1d; // expected-warning {{internal linkage is a C++11 extension}}
63     X1<&f_internal> x1d_addr; // expected-warning {{internal linkage is a C++11 extension}}
64     X2<i> x2a;
65     X2<&i> x2a_addr; // expected-error {{address taken}}
66     X2<iarr> x2b; // expected-error {{cannot bind to template argument of type 'int [10]'}}
67     X2<&iarr> x2b_addr; // expected-error {{address taken}}
68     X2<ki> x2c; // expected-error {{ignores qualifiers}} expected-warning {{internal linkage is a C++11 extension}}
69     X2k<ki> x2kc; // expected-warning {{internal linkage is a C++11 extension}}
70     X2k<&ki> x2kc_addr; // expected-error {{address taken}} expected-warning {{internal linkage is a C++11 extension}}
71     X2<ti> x2d_addr; // expected-error {{refers to thread-local object}}
72     X3<f> x3a;
73     X3<&f> x3a_addr; // expected-error {{address taken}}
74     X3<f_tmpl> x3b;
75     X3<&f_tmpl> x3b_addr; // expected-error {{address taken}}
76     X3<f_tmpl<int> > x3c;
77     X3<&f_tmpl<int> > x3c_addr; // expected-error {{address taken}}
78     X3<f_internal> x3d; // expected-warning {{internal linkage is a C++11 extension}}
79     X3<&f_internal> x3d_addr; // expected-error {{address taken}} expected-warning {{internal linkage is a C++11 extension}}
80 
81     int n; // expected-note {{here}}
82     X0<&n> x0_no_linkage; // expected-error {{non-type template argument refers to object 'n' that does not have linkage}}
83     struct Local { static int f() {} }; // expected-note {{here}}
84     X1<&Local::f> x1_no_linkage; // expected-error {{non-type template argument refers to function 'f' that does not have linkage}}
85     X0<&S::NonStaticMember> x0_non_static; // expected-error {{non-static data member}}
86   }
87 }
88 
89 //   -- a constant expression that evaluates to a null pointer value (4.10); or
90 //   -- a constant expression that evaluates to a null member pointer value
91 //      (4.11); or
92 //   -- a pointer to member expressed as described in 5.3.1.
93 
94 namespace bad_args {
95   template <int* N> struct X0 { }; // expected-note 2{{template parameter is declared here}}
96   int i = 42;
97   X0<&i + 2> x0a; // expected-error{{non-type template argument does not refer to any declaration}}
98   int* iptr = &i;
99   X0<iptr> x0b; // expected-error{{non-type template argument for template parameter of pointer type 'int *' must have its address taken}}
100 }
101 #endif // CPP11ONLY
102 
103 namespace default_args {
104 #ifdef CPP11ONLY
105 namespace lambdas {
106 template<int I = ([] { return 5; }())> //expected-error 2{{constant expression}} expected-note{{constant expression}}
107 int f();
108 }
109 #endif // CPP11ONLY
110 
111 }