Home | History | Annotate | Download | only in optional.object.ctor
      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 // <optional>
     11 
     12 // constexpr optional(const T& v);
     13 
     14 #include <experimental/optional>
     15 #include <type_traits>
     16 #include <cassert>
     17 
     18 #if _LIBCPP_STD_VER > 11
     19 
     20 using std::experimental::optional;
     21 
     22 class X
     23 {
     24     int i_;
     25 public:
     26     X(int i) : i_(i) {}
     27 
     28     friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
     29 };
     30 
     31 class Y
     32 {
     33     int i_;
     34 public:
     35     constexpr Y(int i) : i_(i) {}
     36 
     37     friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
     38 };
     39 
     40 class Z
     41 {
     42     int i_;
     43 public:
     44     Z(int i) : i_(i) {}
     45     Z(const Z&) {throw 6;}
     46 };
     47 
     48 
     49 #endif  // _LIBCPP_STD_VER > 11
     50 
     51 int main()
     52 {
     53 #if _LIBCPP_STD_VER > 11
     54     {
     55         typedef int T;
     56         constexpr T t(5);
     57         constexpr optional<T> opt(t);
     58         static_assert(static_cast<bool>(opt) == true, "");
     59         static_assert(*opt == 5, "");
     60 
     61         struct test_constexpr_ctor
     62             : public optional<T>
     63         {
     64             constexpr test_constexpr_ctor(const T&) {}
     65         };
     66 
     67     }
     68     {
     69         typedef double T;
     70         constexpr T t(3);
     71         constexpr optional<T> opt(t);
     72         static_assert(static_cast<bool>(opt) == true, "");
     73         static_assert(*opt == 3, "");
     74 
     75         struct test_constexpr_ctor
     76             : public optional<T>
     77         {
     78             constexpr test_constexpr_ctor(const T&) {}
     79         };
     80 
     81     }
     82     {
     83         typedef X T;
     84         const T t(3);
     85         optional<T> opt(t);
     86         assert(static_cast<bool>(opt) == true);
     87         assert(*opt == 3);
     88     }
     89     {
     90         typedef Y T;
     91         constexpr T t(3);
     92         constexpr optional<T> opt(t);
     93         static_assert(static_cast<bool>(opt) == true, "");
     94         static_assert(*opt == 3, "");
     95 
     96         struct test_constexpr_ctor
     97             : public optional<T>
     98         {
     99             constexpr test_constexpr_ctor(const T&) {}
    100         };
    101 
    102     }
    103     {
    104         typedef Z T;
    105         try
    106         {
    107             const T t(3);
    108             optional<T> opt(t);
    109             assert(false);
    110         }
    111         catch (int i)
    112         {
    113             assert(i == 6);
    114         }
    115     }
    116 #endif  // _LIBCPP_STD_VER > 11
    117 }
    118