Skip to content

Commit 430c0c9

Browse files
authored
Fix optional::emplace() (#126)
This wasn't marking the newly constructed value present. Also replace unchecked access to _value with checked access for the various operators. This manifested as an exception when running into fallback choices, as the newly emplaced() choice container wasn't valid at runner_impl.cpp(1379).
1 parent 4263031 commit 430c0c9

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

shared/public/system.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,13 @@ class optional
269269
{
270270
}
271271

272-
const T& operator*() const { return _value; }
272+
const T& operator*() const { return value(); }
273273

274-
T& operator*() { return _value; }
274+
T& operator*() { return value(); }
275275

276-
const T* operator->() const { return &_value; }
276+
const T* operator->() const { return &value(); }
277277

278-
T* operator->() { return &_value; }
278+
T* operator->() { return &value(); }
279279

280280
constexpr bool has_value() const { return _has_value; }
281281

@@ -302,8 +302,12 @@ class optional
302302
template<typename... Args>
303303
T& emplace(Args... args)
304304
{
305-
_value.~T();
306-
return *(new (&_value) T(args...));
305+
if (_has_value)
306+
_value.~T();
307+
308+
new (&_value) T(args...);
309+
_has_value = true;
310+
return _value;
307311
}
308312

309313
private:

0 commit comments

Comments
 (0)