libcxx

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

const_optional_U.pass.cpp (2672B)


      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>
     14 //   optional(const optional<U>& rhs);
     15 
     16 #include <optional>
     17 #include <type_traits>
     18 #include <cassert>
     19 
     20 #include "test_macros.h"
     21 
     22 using std::optional;
     23 
     24 template <class T, class U>
     25 void
     26 test(const optional<U>& rhs, bool is_going_to_throw = false)
     27 {
     28     bool rhs_engaged = static_cast<bool>(rhs);
     29 #ifndef TEST_HAS_NO_EXCEPTIONS
     30     try
     31     {
     32         optional<T> lhs = rhs;
     33         assert(is_going_to_throw == false);
     34         assert(static_cast<bool>(lhs) == rhs_engaged);
     35         if (rhs_engaged)
     36             assert(*lhs == *rhs);
     37     }
     38     catch (int i)
     39     {
     40         assert(i == 6);
     41     }
     42 #else
     43     if (is_going_to_throw) return;
     44     optional<T> lhs = rhs;
     45     assert(static_cast<bool>(lhs) == rhs_engaged);
     46     if (rhs_engaged)
     47         assert(*lhs == *rhs);
     48 #endif
     49 }
     50 
     51 class X
     52 {
     53     int i_;
     54 public:
     55     X(int i) : i_(i) {}
     56     X(const X& x) : i_(x.i_) {}
     57     ~X() {i_ = 0;}
     58     friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
     59 };
     60 
     61 class Y
     62 {
     63     int i_;
     64 public:
     65     Y(int i) : i_(i) {}
     66 
     67     friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
     68 };
     69 
     70 int count = 0;
     71 
     72 class Z
     73 {
     74     int i_;
     75 public:
     76     Z(int i) : i_(i) {TEST_THROW(6);}
     77 
     78     friend constexpr bool operator==(const Z& x, const Z& y) {return x.i_ == y.i_;}
     79 };
     80 
     81 
     82 int main()
     83 {
     84     {
     85         typedef short U;
     86         typedef int T;
     87         optional<U> rhs;
     88         test<T>(rhs);
     89     }
     90     {
     91         typedef short U;
     92         typedef int T;
     93         optional<U> rhs(U{3});
     94         test<T>(rhs);
     95     }
     96     {
     97         typedef X T;
     98         typedef int U;
     99         optional<U> rhs;
    100         test<T>(rhs);
    101     }
    102     {
    103         typedef X T;
    104         typedef int U;
    105         optional<U> rhs(U{3});
    106         test<T>(rhs);
    107     }
    108     {
    109         typedef Y T;
    110         typedef int U;
    111         optional<U> rhs;
    112         test<T>(rhs);
    113     }
    114     {
    115         typedef Y T;
    116         typedef int U;
    117         optional<U> rhs(U{3});
    118         test<T>(rhs);
    119     }
    120     {
    121         typedef Z T;
    122         typedef int U;
    123         optional<U> rhs;
    124         test<T>(rhs);
    125     }
    126     {
    127         typedef Z T;
    128         typedef int U;
    129         optional<U> rhs(U{3});
    130         test<T>(rhs, true);
    131     }
    132 
    133     static_assert(!(std::is_constructible<optional<X>, const optional<Y>&>::value), "");
    134 }