From d7ca0bd7dbe371e86fcd53bbce157d9454ce1574 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Thu, 11 Oct 2012 20:01:06 -0400 Subject: [PATCH] maybe.hh: add clear method --- maybe.hh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/maybe.hh b/maybe.hh index 0f88e0b..113404c 100644 --- a/maybe.hh +++ b/maybe.hh @@ -42,9 +42,8 @@ struct maybe { new (&memory) T(*other.as_ptr()); is_init = true; } - } else if (is_init) { - as_ptr()->~T(); - is_init = false; + } else { + clear(); } return *this; } @@ -59,9 +58,8 @@ struct maybe { new (&memory) T(std::move(*other.as_ptr())); is_init = true; } - } else if (is_init) { - as_ptr()->~T(); - is_init = false; + } else { + clear(); } return *this; } @@ -108,6 +106,13 @@ struct maybe { return is_init ? *as_ptr() : v; } + void clear() { + if (is_init) { + as_ptr()->~T(); + is_init = false; + } + } + private: T *as_ptr() { return reinterpret_cast(&memory); } const T *as_ptr() const { return reinterpret_cast(&memory); }