| 1 |
// { dg-do compile { target c++23 } } |
| 2 |
|
| 3 |
// LWG 3886. Monad mo' problems |
| 4 |
|
| 5 |
#include <expected> |
| 6 |
|
| 7 |
void |
| 8 |
test_constructor() |
| 9 |
{ |
| 10 |
struct MoveOnly { |
| 11 |
MoveOnly(int, int) { } |
| 12 |
MoveOnly(MoveOnly&&) { } |
| 13 |
}; |
| 14 |
|
| 15 |
// The {0,0} should be deduced as MoveOnly not const MoveOnly |
| 16 |
[[maybe_unused]] std::expected<const MoveOnly, int> e({0,0}); |
| 17 |
} |
| 18 |
|
| 19 |
struct Tracker { |
| 20 |
bool moved = false; |
| 21 |
constexpr Tracker(int, int) { } |
| 22 |
constexpr Tracker(const Tracker&) { } |
| 23 |
constexpr Tracker(Tracker&&) : moved(true) { } |
| 24 |
|
| 25 |
// The follow means that is_assignable<const Tracker&, U> is true: |
| 26 |
template<typename T> constexpr void operator=(T&&) const { } |
| 27 |
// This stops a copy assignment from being declared implicitly: |
| 28 |
void operator=(Tracker&) = delete; |
| 29 |
}; |
| 30 |
|
| 31 |
void |
| 32 |
test_assignment() |
| 33 |
{ |
| 34 |
constexpr bool moved = [] { |
| 35 |
std::expected<const Tracker, int> e(std::unexpect); |
| 36 |
// The {0,0} should be deduced as Tracker not const Tracker: |
| 37 |
e = {0,0}; |
| 38 |
// So the contained value should have been move constructed not copied: |
| 39 |
return e->moved; |
| 40 |
}(); |
| 41 |
static_assert( moved ); |
| 42 |
} |
| 43 |
|
| 44 |
void |
| 45 |
test_value_or() |
| 46 |
{ |
| 47 |
constexpr bool moved = [] { |
| 48 |
const std::expected<const Tracker, int> e(std::unexpect, 1); |
| 49 |
return e.value_or({0,0}).moved; |
| 50 |
}(); |
| 51 |
static_assert( moved ); |
| 52 |
|
| 53 |
constexpr bool moved_rval = [] { |
| 54 |
std::expected<const Tracker, int> e(std::unexpect, 1); |
| 55 |
return std::move(e).value_or({0,0}).moved; |
| 56 |
}(); |
| 57 |
static_assert( moved_rval ); |
| 58 |
} |