• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++1z -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
5 
6 #if __cplusplus < 201103L
7 // expected-no-diagnostics
8 #endif
9 
10 namespace dr1550 { // dr1550: yes
f(bool b,int n)11   int f(bool b, int n) {
12     return (b ? (throw 0) : n) + (b ? n : (throw 0));
13   }
14 }
15 
16 namespace dr1560 { // dr1560: 3.5
f(bool b,int n)17   void f(bool b, int n) {
18     (b ? throw 0 : n) = (b ? n : throw 0) = 0;
19   }
20   class X { X(const X&); };
21   const X &get();
22   const X &x = true ? get() : throw 0;
23 }
24 
25 #if __cplusplus >= 201103L
26 namespace std {
27   typedef decltype(sizeof(int)) size_t;
28 
29   // libc++'s implementation
30   template <class _E>
31   class initializer_list
32   {
33     const _E* __begin_;
34     size_t    __size_;
35 
initializer_list(const _E * __b,size_t __s)36     initializer_list(const _E* __b, size_t __s)
37     : __begin_(__b), __size_(__s) {}
38 
39   public:
40     typedef _E        value_type;
41     typedef const _E& reference;
42     typedef const _E& const_reference;
43     typedef size_t    size_type;
44 
45     typedef const _E* iterator;
46     typedef const _E* const_iterator;
47 
initializer_list()48     initializer_list() : __begin_(nullptr), __size_(0) {}
49 
size() const50     size_t    size()  const {return __size_;}
begin() const51     const _E* begin() const {return __begin_;}
end() const52     const _E* end()   const {return __begin_ + __size_;}
53   };
54 
55   template < class _T1, class _T2 > struct pair { _T2 second; };
56 
57   template<typename T> struct basic_string {
basic_stringstd::basic_string58     basic_string(const T* x) {}
~basic_stringstd::basic_string59     ~basic_string() {};
60   };
61   typedef basic_string<char> string;
62 
63 } // std
64 
65 namespace dr1589 {   // dr1589: 3.7 c++11
66   // Ambiguous ranking of list-initialization sequences
67 
68   void f0(long, int=0);                 // Would makes selection of #0 ambiguous
69   void f0(long);                        // #0
70   void f0(std::initializer_list<int>);  // #00
g0()71   void g0() { f0({1L}); }               // chooses #00
72 
73   void f1(int, int=0);                    // Would make selection of #1 ambiguous
74   void f1(int);                           // #1
75   void f1(std::initializer_list<long>);   // #2
g1()76   void g1() { f1({42}); }                 // chooses #2
77 
78   void f2(std::pair<const char*, const char*>, int = 0); // Would makes selection of #3 ambiguous
79   void f2(std::pair<const char*, const char*>); // #3
80   void f2(std::initializer_list<std::string>);  // #4
g2()81   void g2() { f2({"foo","bar"}); }              // chooses #4
82 
83   namespace with_error {
84     void f0(long);                        // #0    expected-note {{candidate function}}
85     void f0(std::initializer_list<int>);  // #00   expected-note {{candidate function}}
86     void f0(std::initializer_list<int>, int = 0);  // Makes selection of #00 ambiguous \
87     // expected-note {{candidate function}}
g0()88     void g0() { f0({1L}); }                 // chooses #00    expected-error{{call to 'f0' is ambiguous}}
89 
90     void f1(int);                           // #1   expected-note {{candidate function}}
91     void f1(std::initializer_list<long>);   // #2   expected-note {{candidate function}}
92     void f1(std::initializer_list<long>, int = 0);   // Makes selection of #00 ambiguous \
93     // expected-note {{candidate function}}
g1()94     void g1() { f1({42}); }                 // chooses #2   expected-error{{call to 'f1' is ambiguous}}
95 
96     void f2(std::pair<const char*, const char*>); // #3   TODO: expected- note {{candidate function}}
97     void f2(std::initializer_list<std::string>);  // #4   expected-note {{candidate function}}
98     void f2(std::initializer_list<std::string>, int = 0);   // Makes selection of #00 ambiguous \
99     // expected-note {{candidate function}}
g2()100     void g2() { f2({"foo","bar"}); }        // chooses #4   expected-error{{call to 'f2' is ambiguous}}
101   }
102 
103 } // dr1589
104 
105 namespace dr1591 {  //dr1591. Deducing array bound and element type from initializer list
106   template<class T, int N> int h(T const(&)[N]);
107   int X = h({1,2,3});              // T deduced to int, N deduced to 3
108 
109   template<class T> int j(T const(&)[3]);
110   int Y = j({42});                 // T deduced to int, array bound not considered
111 
112   struct Aggr { int i; int j; };
113   template<int N> int k(Aggr const(&)[N]); //expected-note{{not viable}}
114   int Y0 = k({1,2,3});              //expected-error{{no matching function}}
115   int Z = k({{1},{2},{3}});        // OK, N deduced to 3
116 
117   template<int M, int N> int m(int const(&)[M][N]);
118   int X0 = m({{1,2},{3,4}});        // M and N both deduced to 2
119 
120   template<class T, int N> int n(T const(&)[N], T);
121   int X1 = n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
122 
123 
124   namespace check_multi_dim_arrays {
125     template<class T, int N, int M, int O> int ***f(const T (&a)[N][M][O]); //expected-note{{deduced conflicting values}}
126     template<class T, int N, int M> int **f(const T (&a)[N][M]); //expected-note{{couldn't infer}}
127 
128    template<class T, int N> int *f(const T (&a)[N]); //expected-note{{couldn't infer}}
129     int ***p3 = f({  {  {1,2}, {3, 4}  }, {  {5,6}, {7, 8}  }, {  {9,10}, {11, 12}  } });
130     int ***p33 = f({  {  {1,2}, {3, 4}  }, {  {5,6}, {7, 8}  }, {  {9,10}, {11, 12, 13}  } }); //expected-error{{no matching}}
131     int **p2 = f({  {1,2,3}, {3, 4, 5}  });
132     int **p22 = f({  {1,2}, {3, 4}  });
133     int *p1 = f({1, 2, 3});
134   }
135   namespace check_multi_dim_arrays_rref {
136     template<class T, int N, int M, int O> int ***f(T (&&a)[N][M][O]); //expected-note{{deduced conflicting values}}
137     template<class T, int N, int M> int **f(T (&&a)[N][M]); //expected-note{{couldn't infer}}
138 
139     template<class T, int N> int *f(T (&&a)[N]); //expected-note{{couldn't infer}}
140     int ***p3 = f({  {  {1,2}, {3, 4}  }, {  {5,6}, {7, 8}  }, {  {9,10}, {11, 12}  } });
141     int ***p33 = f({  {  {1,2}, {3, 4}  }, {  {5,6}, {7, 8}  }, {  {9,10}, {11, 12, 13}  } }); //expected-error{{no matching}}
142     int **p2 = f({  {1,2,3}, {3, 4, 5}  });
143     int **p22 = f({  {1,2}, {3, 4}  });
144     int *p1 = f({1, 2, 3});
145   }
146 
147   namespace check_arrays_of_init_list {
148     template<class T, int N> float *f(const std::initializer_list<T> (&)[N]);
149     template<class T, int N> double *f(const T(&)[N]);
150     double *p = f({1, 2, 3});
151     float *fp = f({{1}, {1, 2}, {1, 2, 3}});
152   }
153   namespace core_reflector_28543 {
154 
155     template<class T, int N> int *f(T (&&)[N]);  // #1
156     template<class T> char *f(std::initializer_list<T> &&);  //#2
157     template<class T, int N, int M> int **f(T (&&)[N][M]); //#3 expected-note{{candidate}}
158     template<class T, int N> char **f(std::initializer_list<T> (&&)[N]); //#4 expected-note{{candidate}}
159 
160     template<class T> short *f(T (&&)[2]);  //#5
161 
162     template<class T> using Arr = T[];
163 
164     char *pc = f({1, 2, 3}); // OK prefer #2 via 13.3.3.2 [over.ics.rank]
165     char *pc2 = f({1, 2}); // #2 also
166     int *pi = f(Arr<int>{1, 2, 3}); // OK prefer #1
167 
168     void *pv1 = f({ {1, 2, 3}, {4, 5, 6} }); // expected-error{{ambiguous}} btw 3 & 4
169     char **pcc = f({ {1}, {2, 3} }); // OK #4
170 
171     short *ps = f(Arr<int>{1, 2});  // OK #5
172   }
173 } // dr1591
174 
175 #endif
176