libcxx

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

in_place_t.pass.cpp (3396B)


      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 
     12 // <optional>
     13 
     14 // template <class... Args>
     15 //   constexpr explicit optional(in_place_t, Args&&... args);
     16 
     17 #include <optional>
     18 #include <type_traits>
     19 #include <cassert>
     20 
     21 #include "test_macros.h"
     22 
     23 using std::optional;
     24 using std::in_place_t;
     25 using std::in_place;
     26 
     27 class X
     28 {
     29     int i_;
     30     int j_ = 0;
     31 public:
     32     X() : i_(0) {}
     33     X(int i) : i_(i) {}
     34     X(int i, int j) : i_(i), j_(j) {}
     35 
     36     ~X() {}
     37 
     38     friend bool operator==(const X& x, const X& y)
     39         {return x.i_ == y.i_ && x.j_ == y.j_;}
     40 };
     41 
     42 class Y
     43 {
     44     int i_;
     45     int j_ = 0;
     46 public:
     47     constexpr Y() : i_(0) {}
     48     constexpr Y(int i) : i_(i) {}
     49     constexpr Y(int i, int j) : i_(i), j_(j) {}
     50 
     51     friend constexpr bool operator==(const Y& x, const Y& y)
     52         {return x.i_ == y.i_ && x.j_ == y.j_;}
     53 };
     54 
     55 class Z
     56 {
     57 public:
     58     Z(int) {TEST_THROW(6);}
     59 };
     60 
     61 
     62 int main()
     63 {
     64     {
     65         constexpr optional<int> opt(in_place, 5);
     66         static_assert(static_cast<bool>(opt) == true, "");
     67         static_assert(*opt == 5, "");
     68 
     69         struct test_constexpr_ctor
     70             : public optional<int>
     71         {
     72             constexpr test_constexpr_ctor(in_place_t, int i)
     73                 : optional<int>(in_place, i) {}
     74         };
     75 
     76     }
     77     {
     78         optional<const int> opt(in_place, 5);
     79         assert(*opt == 5);
     80     }
     81     {
     82         const optional<X> opt(in_place);
     83         assert(static_cast<bool>(opt) == true);
     84         assert(*opt == X());
     85     }
     86     {
     87         const optional<X> opt(in_place, 5);
     88         assert(static_cast<bool>(opt) == true);
     89         assert(*opt == X(5));
     90     }
     91     {
     92         const optional<X> opt(in_place, 5, 4);
     93         assert(static_cast<bool>(opt) == true);
     94         assert(*opt == X(5, 4));
     95     }
     96     {
     97         constexpr optional<Y> opt(in_place);
     98         static_assert(static_cast<bool>(opt) == true, "");
     99         static_assert(*opt == Y(), "");
    100 
    101         struct test_constexpr_ctor
    102             : public optional<Y>
    103         {
    104             constexpr test_constexpr_ctor(in_place_t)
    105                 : optional<Y>(in_place) {}
    106         };
    107 
    108     }
    109     {
    110         constexpr optional<Y> opt(in_place, 5);
    111         static_assert(static_cast<bool>(opt) == true, "");
    112         static_assert(*opt == Y(5), "");
    113 
    114         struct test_constexpr_ctor
    115             : public optional<Y>
    116         {
    117             constexpr test_constexpr_ctor(in_place_t, int i)
    118                 : optional<Y>(in_place, i) {}
    119         };
    120 
    121     }
    122     {
    123         constexpr optional<Y> opt(in_place, 5, 4);
    124         static_assert(static_cast<bool>(opt) == true, "");
    125         static_assert(*opt == Y(5, 4), "");
    126 
    127         struct test_constexpr_ctor
    128             : public optional<Y>
    129         {
    130             constexpr test_constexpr_ctor(in_place_t, int i, int j)
    131                 : optional<Y>(in_place, i, j) {}
    132         };
    133 
    134     }
    135 #ifndef TEST_HAS_NO_EXCEPTIONS
    136     {
    137         try
    138         {
    139             const optional<Z> opt(in_place, 1);
    140             assert(false);
    141         }
    142         catch (int i)
    143         {
    144             assert(i == 6);
    145         }
    146     }
    147 #endif
    148 }