Home | History | Annotate | Download | only in optional.object.assign
      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 //   T& optional<T>::emplace(initializer_list<U> il, Args&&... args);
     15 
     16 #include <optional>
     17 #include <type_traits>
     18 #include <cassert>
     19 #include <vector>
     20 
     21 #include "test_macros.h"
     22 
     23 using std::optional;
     24 
     25 class X
     26 {
     27     int i_;
     28     int j_ = 0;
     29 public:
     30     static bool dtor_called;
     31     constexpr X() : i_(0) {}
     32     constexpr X(int i) : i_(i) {}
     33     constexpr X(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
     34     ~X() {dtor_called = true;}
     35 
     36     friend constexpr bool operator==(const X& x, const X& y)
     37         {return x.i_ == y.i_ && x.j_ == y.j_;}
     38 };
     39 
     40 bool X::dtor_called = false;
     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(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {}
     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     int i_;
     58     int j_ = 0;
     59 public:
     60     static bool dtor_called;
     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     ~Z() {dtor_called = true;}
     66 
     67     friend bool operator==(const Z& x, const Z& y)
     68         {return x.i_ == y.i_ && x.j_ == y.j_;}
     69 };
     70 
     71 bool Z::dtor_called = false;
     72 
     73 int main()
     74 {
     75     {
     76         X x;
     77         optional<X> opt(x);
     78         assert(X::dtor_called == false);
     79         auto &v = opt.emplace({1, 2});
     80         static_assert( std::is_same_v<X&, decltype(v)>, "" );
     81         assert(X::dtor_called == true);
     82         assert(*opt == X({1, 2}));
     83         assert(&v == &*opt);
     84     }
     85     {
     86         optional<std::vector<int>> opt;
     87         auto &v = opt.emplace({1, 2, 3}, std::allocator<int>());
     88         static_assert( std::is_same_v<std::vector<int>&, decltype(v)>, "" );
     89         assert(static_cast<bool>(opt) == true);
     90         assert(*opt == std::vector<int>({1, 2, 3}));
     91         assert(&v == &*opt);
     92     }
     93     {
     94         optional<Y> opt;
     95         auto &v = opt.emplace({1, 2});
     96         static_assert( std::is_same_v<Y&, decltype(v)>, "" );
     97         assert(static_cast<bool>(opt) == true);
     98         assert(*opt == Y({1, 2}));
     99         assert(&v == &*opt);
    100     }
    101 #ifndef TEST_HAS_NO_EXCEPTIONS
    102     {
    103         Z z;
    104         optional<Z> opt(z);
    105         try
    106         {
    107             assert(static_cast<bool>(opt) == true);
    108             assert(Z::dtor_called == false);
    109             auto &v = opt.emplace({1, 2});
    110             static_assert( std::is_same_v<Z&, decltype(v)>, "" );
    111             assert(false);
    112         }
    113         catch (int i)
    114         {
    115             assert(i == 6);
    116             assert(static_cast<bool>(opt) == false);
    117             assert(Z::dtor_called == true);
    118         }
    119     }
    120 #endif
    121 }
    122