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 // UNSUPPORTED: c++98, c++03, c++11, c++14
     11 
     12 // <optional>
     13 
     14 // constexpr optional(T&& v);
     15 
     16 #include <optional>
     17 #include <type_traits>
     18 #include <cassert>
     19 
     20 #include "test_macros.h"
     21 #include "archetypes.hpp"
     22 
     23 
     24 using std::optional;
     25 
     26 
     27 class Z
     28 {
     29 public:
     30     Z(int) {}
     31     Z(Z&&) {TEST_THROW(6);}
     32 };
     33 
     34 
     35 int main()
     36 {
     37     {
     38         typedef int T;
     39         constexpr optional<T> opt(T(5));
     40         static_assert(static_cast<bool>(opt) == true, "");
     41         static_assert(*opt == 5, "");
     42 
     43         struct test_constexpr_ctor
     44             : public optional<T>
     45         {
     46             constexpr test_constexpr_ctor(T&&) {}
     47         };
     48     }
     49     {
     50         typedef double T;
     51         constexpr optional<T> opt(T(3));
     52         static_assert(static_cast<bool>(opt) == true, "");
     53         static_assert(*opt == 3, "");
     54 
     55         struct test_constexpr_ctor
     56             : public optional<T>
     57         {
     58             constexpr test_constexpr_ctor(T&&) {}
     59         };
     60     }
     61     {
     62         const int x = 42;
     63         optional<const int> o(std::move(x));
     64         assert(*o == 42);
     65     }
     66     {
     67         typedef TestTypes::TestType T;
     68         T::reset();
     69         optional<T> opt = T{3};
     70         assert(T::alive == 1);
     71         assert(T::move_constructed == 1);
     72         assert(static_cast<bool>(opt) == true);
     73         assert(opt.value().value == 3);
     74     }
     75     {
     76         typedef ExplicitTestTypes::TestType T;
     77         static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
     78         T::reset();
     79         optional<T> opt(T{3});
     80         assert(T::alive == 1);
     81         assert(T::move_constructed == 1);
     82         assert(static_cast<bool>(opt) == true);
     83         assert(opt.value().value == 3);
     84     }
     85     {
     86         typedef TestTypes::TestType T;
     87         T::reset();
     88         optional<T> opt = {3};
     89         assert(T::alive == 1);
     90         assert(T::value_constructed == 1);
     91         assert(T::copy_constructed == 0);
     92         assert(T::move_constructed == 0);
     93         assert(static_cast<bool>(opt) == true);
     94         assert(opt.value().value == 3);
     95     }
     96     {
     97         typedef ConstexprTestTypes::TestType T;
     98         constexpr optional<T> opt = {T(3)};
     99         static_assert(static_cast<bool>(opt) == true, "");
    100         static_assert(opt.value().value == 3, "");
    101 
    102         struct test_constexpr_ctor
    103             : public optional<T>
    104         {
    105             constexpr test_constexpr_ctor(const T&) {}
    106         };
    107     }
    108     {
    109         typedef ConstexprTestTypes::TestType T;
    110         constexpr optional<T> opt = {3};
    111         static_assert(static_cast<bool>(opt) == true, "");
    112         static_assert(opt.value().value == 3, "");
    113 
    114         struct test_constexpr_ctor
    115             : public optional<T>
    116         {
    117             constexpr test_constexpr_ctor(const T&) {}
    118         };
    119     }
    120     {
    121         typedef ExplicitConstexprTestTypes::TestType T;
    122         static_assert(!std::is_convertible<T&&, optional<T>>::value, "");
    123         constexpr optional<T> opt(T{3});
    124         static_assert(static_cast<bool>(opt) == true, "");
    125         static_assert(opt.value().value == 3, "");
    126 
    127         struct test_constexpr_ctor
    128             : public optional<T>
    129         {
    130             constexpr test_constexpr_ctor(T&&) {}
    131         };
    132 
    133     }
    134 #ifndef TEST_HAS_NO_EXCEPTIONS
    135     {
    136         struct Z {
    137             Z(int) {}
    138             Z(Z&&) {throw 6;}
    139         };
    140         typedef Z T;
    141         try
    142         {
    143             T t(3);
    144             optional<T> opt(std::move(t));
    145             assert(false);
    146         }
    147         catch (int i)
    148         {
    149             assert(i == 6);
    150         }
    151     }
    152 #endif
    153 }
    154