libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

initializer_list.pass.cpp (3025B)


      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03, c++11, c++14
     11 // <optional>
     12 
     13 // template <class U, class... Args>
     14 //     constexpr
     15 //     explicit optional(in_place_t, initializer_list<U> il, Args&&... args);
     16 
     17 #include <optional>
     18 #include <type_traits>
     19 #include <vector>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 
     24 using std::optional;
     25 using std::in_place_t;
     26 using std::in_place;
     27 
     28 class X
     29 {
     30     int i_;
     31     int j_ = 0;
     32 public:
     33     X() : i_(0) {}
     34     X(int i) : i_(i) {}
     35     X(int i, int j) : i_(i), j_(j) {}
     36 
     37     ~X() {}
     38 
     39     friend bool operator==(const X& x, const X& y)
     40         {return x.i_ == y.i_ && x.j_ == y.j_;}
     41 };
     42 
     43 class Y
     44 {
     45     int i_;
     46     int j_ = 0;
     47 public:
     48     constexpr Y() : i_(0) {}
     49     constexpr Y(int i) : i_(i) {}
     50     constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
     51 
     52     friend constexpr bool operator==(const Y& x, const Y& y)
     53         {return x.i_ == y.i_ && x.j_ == y.j_;}
     54 };
     55 
     56 class Z
     57 {
     58     int i_;
     59     int j_ = 0;
     60 public:
     61     Z() : i_(0) {}
     62     Z(int i) : i_(i) {}
     63     Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1])
     64         {TEST_THROW(6);}
     65 
     66     friend bool operator==(const Z& x, const Z& y)
     67         {return x.i_ == y.i_ && x.j_ == y.j_;}
     68 };
     69 
     70 int main()
     71 {
     72     {
     73         static_assert(!std::is_constructible<X, std::initializer_list<int>&>::value, "");
     74         static_assert(!std::is_constructible<optional<X>, std::initializer_list<int>&>::value, "");
     75     }
     76     {
     77         optional<std::vector<int>> opt(in_place, {3, 1});
     78         assert(static_cast<bool>(opt) == true);
     79         assert((*opt == std::vector<int>{3, 1}));
     80         assert(opt->size() == 2);
     81     }
     82     {
     83         optional<std::vector<int>> opt(in_place, {3, 1}, std::allocator<int>());
     84         assert(static_cast<bool>(opt) == true);
     85         assert((*opt == std::vector<int>{3, 1}));
     86         assert(opt->size() == 2);
     87     }
     88     {
     89         static_assert(std::is_constructible<optional<Y>, std::initializer_list<int>&>::value, "");
     90         constexpr optional<Y> opt(in_place, {3, 1});
     91         static_assert(static_cast<bool>(opt) == true, "");
     92         static_assert(*opt == Y{3, 1}, "");
     93 
     94         struct test_constexpr_ctor
     95             : public optional<Y>
     96         {
     97             constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i)
     98                 : optional<Y>(in_place, i) {}
     99         };
    100 
    101     }
    102 #ifndef TEST_HAS_NO_EXCEPTIONS
    103     {
    104         static_assert(std::is_constructible<optional<Z>, std::initializer_list<int>&>::value, "");
    105         try
    106         {
    107             optional<Z> opt(in_place, {3, 1});
    108             assert(false);
    109         }
    110         catch (int i)
    111         {
    112             assert(i == 6);
    113         }
    114     }
    115 #endif
    116 }