Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
      2 // XFAIL: *
      3 
      4 template <typename T, typename U>
      5 struct same_type { static const bool value = false; };
      6 template <typename T>
      7 struct same_type<T, T> { static const bool value = true; };
      8 
      9 namespace std {
     10   typedef decltype(sizeof(int)) size_t;
     11 
     12   // libc++'s implementation
     13   template <class _E>
     14   class initializer_list
     15   {
     16     const _E* __begin_;
     17     size_t    __size_;
     18 
     19     initializer_list(const _E* __b, size_t __s)
     20       : __begin_(__b),
     21         __size_(__s)
     22     {}
     23 
     24   public:
     25     typedef _E        value_type;
     26     typedef const _E& reference;
     27     typedef const _E& const_reference;
     28     typedef size_t    size_type;
     29 
     30     typedef const _E* iterator;
     31     typedef const _E* const_iterator;
     32 
     33     initializer_list() : __begin_(nullptr), __size_(0) {}
     34 
     35     size_t    size()  const {return __size_;}
     36     const _E* begin() const {return __begin_;}
     37     const _E* end()   const {return __begin_ + __size_;}
     38   };
     39 }
     40 
     41 namespace integral {
     42 
     43   void initializer_list() {
     44     std::initializer_list<int> il = { 1, 2, 3 };
     45     std::initializer_list<double> dl = { 1.0, 2.0, 3 };
     46     auto l = {1, 2, 3, 4};
     47     static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
     48     auto bl = {1, 2.0}; // expected-error {{cannot deduce}}
     49 
     50     for (int i : {1, 2, 3, 4}) {}
     51   }
     52 
     53 }
     54 
     55 namespace objects {
     56 
     57   struct X1 { X1(int); };
     58   struct X2 { explicit X2(int); };
     59 
     60   template <int N>
     61   struct A {
     62     A() { static_assert(N == 0, ""); }
     63     A(int, double) { static_assert(N == 1, ""); }
     64     A(std::initializer_list<int>) { static_assert(N == 3, ""); }
     65   };
     66 
     67   template <int N>
     68   struct D {
     69     D(std::initializer_list<int>) { static_assert(N == 0, ""); } // expected-note 1 {{candidate}}
     70     D(std::initializer_list<double>) { static_assert(N == 1, ""); } // expected-note 1 {{candidate}}
     71   };
     72 
     73   template <int N>
     74   struct E {
     75     E(int, int) { static_assert(N == 0, ""); }
     76     E(X1, int) { static_assert(N == 1, ""); }
     77   };
     78 
     79   void overload_resolution() {
     80     { A<0> a{}; }
     81     { A<0> a = {}; }
     82     // Narrowing conversions don't affect viability. The next two choose
     83     // the initializer_list constructor.
     84     { A<3> a{1, 1.0}; } // expected-error {{narrowing conversion}}
     85     { A<3> a = {1, 1.0}; } // expected-error {{narrowing conversion}}
     86     { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; }
     87     { A<3> a = {1, 2, 3, 4, 5, 6, 7, 8}; }
     88     { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; }
     89     { A<3> a{1, 2}; }
     90 
     91     { D<0> d{1, 2, 3}; }
     92     { D<1> d{1.0, 2.0, 3.0}; }
     93     { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}}
     94 
     95     { E<0> e{1, 2}; }
     96   }
     97 
     98   void explicit_implicit() {
     99     { X1 x{0}; }
    100     { X1 x = {0}; }
    101     { X2 x{0}; }
    102     { X2 x = {0}; } // expected-error {{explicit}}
    103   }
    104 
    105   struct C {
    106     C();
    107     C(int, double);
    108     C(int, int);
    109     C(std::initializer_list<int>);
    110 
    111     int operator[](C);
    112   };
    113 
    114   C function_call() {
    115     void takes_C(C);
    116     takes_C({1, 1.0});
    117 
    118     C c;
    119     c[{1, 1.0}];
    120 
    121     return {1, 1.0};
    122   }
    123 
    124   void inline_init() {
    125     (void) A<1>{1, 1.0};
    126     (void) new A<1>{1, 1.0};
    127   }
    128 
    129   struct B {
    130     B(C, int, C);
    131   };
    132 
    133   void nested_init() {
    134     B b{{1, 1.0}, 2, {3, 4, 5, 6, 7}};
    135   }
    136 }
    137 
    138 namespace litb {
    139 
    140   // invalid
    141   struct A { int a[2]; A():a({1, 2}) { } }; // expected-error {{}}
    142 
    143   // invalid
    144   int a({0}); // expected-error {{}}
    145 
    146   // invalid
    147   int const &b({0}); // expected-error {{}}
    148 
    149   struct C { explicit C(int, int); C(int, long); };
    150 
    151   // invalid
    152   C c({1, 2}); // expected-error {{}}
    153 
    154   // valid (by copy constructor).
    155   C d({1, 2L}); // expected-error {{}}
    156 
    157   // valid
    158   C e{1, 2};
    159 
    160   struct B {
    161     template<typename ...T>
    162     B(std::initializer_list<int>, T ...);
    163   };
    164 
    165   // invalid (the first phase only considers init-list ctors)
    166   // (for the second phase, no constructor is viable)
    167   B f{1, 2, 3};
    168 
    169   // valid (T deduced to <>).
    170   B g({1, 2, 3});
    171 
    172 }
    173