Commit 320f4ef1 authored by Kenton Varda's avatar Kenton Varda

Maybe::emplace() should return the newly-constructed value so that you don't…

Maybe::emplace() should return the newly-constructed value so that you don't have to immediately KJ_ASSERT_NONNULL it.
parent 17020509
......@@ -810,13 +810,14 @@ public:
inline operator const T*() const { return isSet ? &value : nullptr; }
template <typename... Params>
inline void emplace(Params&&... params) {
inline T& emplace(Params&&... params) {
if (isSet) {
isSet = false;
dtor(value);
}
ctor(value, kj::fwd<Params>(params)...);
isSet = true;
return value;
}
private: // internal interface used by friends only
......@@ -982,11 +983,12 @@ public:
Maybe(decltype(nullptr)) noexcept: ptr(nullptr) {}
template <typename... Params>
inline void emplace(Params&&... params) {
inline T& emplace(Params&&... params) {
// Replace this Maybe's content with a new value constructed by passing the given parametrs to
// T's constructor. This can be used to initialize a Maybe without copying or even moving a T.
// Returns a reference to the newly-constructed value.
ptr.emplace(kj::fwd<Params>(params)...);
return ptr.emplace(kj::fwd<Params>(params)...);
}
inline Maybe& operator=(Maybe&& other) { ptr = kj::mv(other.ptr); return *this; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment