mirror of
https://github.com/TartanLlama/expected.git
synced 2025-08-05 20:04:30 +02:00
@@ -1680,19 +1680,20 @@ public:
|
|||||||
T, Args &&...>::value> * = nullptr>
|
T, Args &&...>::value> * = nullptr>
|
||||||
void emplace(Args &&... args) {
|
void emplace(Args &&... args) {
|
||||||
if (has_value()) {
|
if (has_value()) {
|
||||||
val() = T(std::forward<Args>(args)...);
|
val().~T();
|
||||||
} else {
|
} else {
|
||||||
err().~unexpected<E>();
|
err().~unexpected<E>();
|
||||||
::new (valptr()) T(std::forward<Args>(args)...);
|
|
||||||
this->m_has_val = true;
|
this->m_has_val = true;
|
||||||
}
|
}
|
||||||
|
::new (valptr()) T(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class... Args, detail::enable_if_t<!std::is_nothrow_constructible<
|
template <class... Args, detail::enable_if_t<!std::is_nothrow_constructible<
|
||||||
T, Args &&...>::value> * = nullptr>
|
T, Args &&...>::value> * = nullptr>
|
||||||
void emplace(Args &&... args) {
|
void emplace(Args &&... args) {
|
||||||
if (has_value()) {
|
if (has_value()) {
|
||||||
val() = T(std::forward<Args>(args)...);
|
val().~T();
|
||||||
|
::new (valptr()) T(std::forward<Args>(args)...);
|
||||||
} else {
|
} else {
|
||||||
auto tmp = std::move(err());
|
auto tmp = std::move(err());
|
||||||
err().~unexpected<E>();
|
err().~unexpected<E>();
|
||||||
|
@@ -136,3 +136,27 @@ tl::expected<int, std::unique_ptr<std::string>> func()
|
|||||||
TEST_CASE("Issue 61", "[issues.61]") {
|
TEST_CASE("Issue 61", "[issues.61]") {
|
||||||
REQUIRE(func().value() == 1);
|
REQUIRE(func().value() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct move_tracker {
|
||||||
|
int moved = 0;
|
||||||
|
|
||||||
|
constexpr move_tracker() = default;
|
||||||
|
|
||||||
|
constexpr move_tracker(move_tracker const &other) noexcept = default;
|
||||||
|
constexpr move_tracker(move_tracker &&orig) noexcept
|
||||||
|
: moved(orig.moved + 1) {}
|
||||||
|
|
||||||
|
constexpr move_tracker &
|
||||||
|
operator=(move_tracker const &other) noexcept = default;
|
||||||
|
|
||||||
|
constexpr move_tracker &operator=(move_tracker &&orig) noexcept {
|
||||||
|
moved = orig.moved + 1;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_CASE("Issue 122", "[issues.122]") {
|
||||||
|
tl::expected<move_tracker, int> res;
|
||||||
|
res.emplace(); // why moved?
|
||||||
|
REQUIRE(res.value().moved == 0);
|
||||||
|
}
|
Reference in New Issue
Block a user