|
| 1 | +#include <typeinfo> |
| 2 | +#include <utility> |
| 3 | + |
| 4 | +#include <mapbox/variant.hpp> |
| 5 | + |
| 6 | +using namespace mapbox; |
| 7 | + |
| 8 | +namespace test { |
| 9 | + |
| 10 | +struct t_noexcept_true_1 { |
| 11 | + t_noexcept_true_1(t_noexcept_true_1&&) noexcept = default; |
| 12 | + t_noexcept_true_1& operator=(t_noexcept_true_1&&) noexcept = default; |
| 13 | +}; |
| 14 | + |
| 15 | +struct t_noexcept_true_2 { |
| 16 | + t_noexcept_true_2(t_noexcept_true_2&&) noexcept = default; |
| 17 | + t_noexcept_true_2& operator=(t_noexcept_true_2&&) noexcept = default; |
| 18 | +}; |
| 19 | + |
| 20 | +struct t_noexcept_false_1 { |
| 21 | + t_noexcept_false_1(t_noexcept_false_1&&) noexcept(false) {} |
| 22 | + t_noexcept_false_1& operator=(t_noexcept_false_1&&) noexcept(false) { return *this; } |
| 23 | +}; |
| 24 | + |
| 25 | +using should_be_no_throw_copyable = util::variant<t_noexcept_true_1, t_noexcept_true_2>; |
| 26 | +static_assert(std::is_nothrow_move_assignable<should_be_no_throw_copyable>::value, |
| 27 | + "variants with no-throw move assignable types should be " |
| 28 | + "no-throw move nothrow assignable"); |
| 29 | + |
| 30 | +using should_be_no_throw_assignable = util::variant<t_noexcept_true_1, t_noexcept_true_2>; |
| 31 | +static_assert(std::is_nothrow_move_constructible<should_be_no_throw_assignable>::value, |
| 32 | + "variants with no-throw move assignable types should be " |
| 33 | + "no-throw move nothrow assignable"); |
| 34 | + |
| 35 | +using should_not_be_no_throw_copyable = util::variant<t_noexcept_true_1, t_noexcept_false_1>; |
| 36 | +static_assert(not std::is_nothrow_move_assignable<should_not_be_no_throw_copyable>::value, |
| 37 | + "variants with no-throw move assignable types should be " |
| 38 | + "no-throw move nothrow assignable"); |
| 39 | + |
| 40 | +using should_not_be_no_throw_assignable = util::variant<t_noexcept_true_1, t_noexcept_false_1>; |
| 41 | +static_assert(not std::is_nothrow_move_constructible<should_not_be_no_throw_assignable>::value, |
| 42 | + "variants with no-throw move assignable types should be " |
| 43 | + "no-throw move nothrow assignable"); |
| 44 | + |
| 45 | + |
| 46 | +// this type cannot be nothrow converted from either of its types, even the nothrow moveable one, |
| 47 | +// because the conversion operator moves the whole variant. |
| 48 | +using convertable_test_type = util::variant<t_noexcept_true_1, t_noexcept_false_1>; |
| 49 | + |
| 50 | +// this type can be nothrow converted from either of its types. |
| 51 | +using convertable_test_type_2 = util::variant<t_noexcept_true_1, t_noexcept_true_2>; |
| 52 | + |
| 53 | +static_assert(not std::is_nothrow_assignable<convertable_test_type, t_noexcept_true_1>::value, |
| 54 | + "variants with noexcept(true) move constructible types should be nothrow-convertible " |
| 55 | + "from those types only IF the variant itself is nothrow_move_assignable"); |
| 56 | + |
| 57 | +static_assert(not std::is_nothrow_assignable<convertable_test_type, t_noexcept_false_1>::value, |
| 58 | + "variants with noexcept(false) move constructible types should not be nothrow-convertible " |
| 59 | + "from those types"); |
| 60 | + |
| 61 | +static_assert(std::is_nothrow_assignable<convertable_test_type_2, t_noexcept_true_2>::value, |
| 62 | + "variants with noexcept(true) move constructible types should be nothrow-convertible " |
| 63 | + "from those types only IF the variant itself is nothrow_move_assignable"); |
| 64 | + |
| 65 | + |
| 66 | +} // namespace test |
0 commit comments