state!");
}
} else {
destroy();
_tag = other._tag;
switch (_tag) {
case Tag::Ok: new (&_value.ok) T(other._value.ok); break;
case Tag::Err: new (&_value.error) E(other._value.error); break;
default:
panic("[_tag] was left in an invalid state!");
}
}
return *this;
}
Result &operator=(Result &&other) noexcept {
if (this == &other) {
return *this;
}
if (_tag == other._tag) {
switch (_tag) {
case Tag::Ok: _value.ok = move(other._value.ok); break;
case Tag::Err: _value.error = move(other._value.error); break;
default:
panic("[_tag] was left in an invalid state!");
}
} else {
destroy();
_tag = other._tag;
switch (_tag) {
case Tag::Ok: new (&_value.ok) T(move(other._value.ok)); break;
case Tag::Err: new (&_value.error) E(move(other._value.error)); break;
default:
panic("[_tag] was left in an invalid state!");
}
}
return *this;
}
private:
enum class Tag {
Ok,
Err
} _tag;
union Variant {
T ok;
E error;
constexpr Variant() noexcept {}
~Variant() noexcept {}
} _value;
explicit Result(Tag tag, const T &ok, OkOverload)
: _tag{ tag } {
new (&_value.ok) T(ok);
}
explicit Result(Tag tag, const E &error, ErrOverload)
: _tag{ tag } {
new (&_value.error) E(error);
}
explicit Result(Tag tag, T &&ok, OkOverload)
: _tag{ tag } {
new (&_value.ok) T(move(ok));
}
explicit Result(Tag tag, E &&error, ErrOverload)
: _tag{ tag } {
new (&_value.error) E(move(error));
}
void destroy() noexcept {
switch (_tag) {
case Tag::Ok: _value.ok.~T(); break;
case Tag::Err: _value.error.~E(); break;
default:
panic("[_tag] was left in an invalid state!");
}
}
};
struct Unit {};
template<typename T>
using Option = Result<T, Unit>;
template<typename E>
using Fallible = Result<Unit, E>;
#endif
```
https://redd.it/1okpb9k
@r_cpp
Post #24150
12