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
     11 // <optional>
     12 
     13 // optional<T>& operator=(const optional<T>& rhs);
     14 
     15 #include <experimental/optional>
     16 #include <type_traits>
     17 #include <cassert>
     18 
     19 #include "test_macros.h"
     20 
     21 using std::experimental::optional;
     22 
     23 struct AllowConstAssign {
     24   AllowConstAssign(AllowConstAssign const&) {}
     25   AllowConstAssign const& operator=(AllowConstAssign const&) const {
     26       return *this;
     27   }
     28 };
     29 
     30 struct X
     31 {
     32     static bool throw_now;
     33 
     34     X() = default;
     35     X(const X&)
     36     {
     37         if (throw_now)
     38             TEST_THROW(6);
     39     }
     40 };
     41 
     42 bool X::throw_now = false;
     43 
     44 int main()
     45 {
     46     {
     47         optional<int> opt;
     48         constexpr optional<int> opt2;
     49         opt = opt2;
     50         static_assert(static_cast<bool>(opt2) == false, "");
     51         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
     52     }
     53     {
     54         optional<const AllowConstAssign> opt;
     55         optional<const AllowConstAssign> opt2;
     56         opt = opt2;
     57     }
     58     {
     59         optional<int> opt;
     60         constexpr optional<int> opt2(2);
     61         opt = opt2;
     62         static_assert(static_cast<bool>(opt2) == true, "");
     63         static_assert(*opt2 == 2, "");
     64         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
     65         assert(*opt == *opt2);
     66     }
     67     {
     68         optional<int> opt(3);
     69         constexpr optional<int> opt2;
     70         opt = opt2;
     71         static_assert(static_cast<bool>(opt2) == false, "");
     72         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
     73     }
     74     {
     75         optional<int> opt(3);
     76         constexpr optional<int> opt2(2);
     77         opt = opt2;
     78         static_assert(static_cast<bool>(opt2) == true, "");
     79         static_assert(*opt2 == 2, "");
     80         assert(static_cast<bool>(opt) == static_cast<bool>(opt2));
     81         assert(*opt == *opt2);
     82     }
     83 #ifndef TEST_HAS_NO_EXCEPTIONS
     84     {
     85         optional<X> opt;
     86         optional<X> opt2(X{});
     87         assert(static_cast<bool>(opt2) == true);
     88         try
     89         {
     90             X::throw_now = true;
     91             opt = opt2;
     92             assert(false);
     93         }
     94         catch (int i)
     95         {
     96             assert(i == 6);
     97             assert(static_cast<bool>(opt) == false);
     98         }
     99     }
    100 #endif
    101 }
    102