From 7764f2717082a77cff63da24827355c7200c2979 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 10 Mar 2018 02:10:40 -0800 Subject: [PATCH] Correct failure to set 'filled' flag in maybe_t constructors --- src/fish_tests.cpp | 7 +++++++ src/maybe.h | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index e2d70a73e..3ccd7ad25 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -4472,6 +4472,13 @@ void test_maybe() { do_test(m2.missing_or_empty()); m2 = none(); do_test(m2.missing_or_empty()); + + maybe_t m0 = none(); + maybe_t m3("hi"); + maybe_t m4 = m3; + do_test(m4 && *m4 == "hi"); + maybe_t m5 = m0; + do_test(!m5); } void test_layout_cache() { diff --git a/src/maybe.h b/src/maybe.h index 73939471c..898731f9d 100644 --- a/src/maybe.h +++ b/src/maybe.h @@ -40,16 +40,16 @@ class maybe_t { /* implicit */ maybe_t(const T &v) : filled(true) { new (storage) T(v); } // Copy constructor. - maybe_t(const maybe_t &v) { + maybe_t(const maybe_t &v) : filled(v.filled) { if (v.filled) { new (storage) T(v.value()); } } // Move constructor. - /* implicit */ maybe_t(maybe_t &&v) { + /* implicit */ maybe_t(maybe_t &&v) : filled(v.filled) { if (v.filled) { - *this = std::move(v.value()); + new (storage) T(std::move(v.value())); } }