Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Commit

Permalink
maybe.hh: add clear method
Browse files Browse the repository at this point in the history
  • Loading branch information
thestinger committed Oct 12, 2012
1 parent 1245563 commit d7ca0bd
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions maybe.hh
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<T *>(&memory); }
const T *as_ptr() const { return reinterpret_cast<const T *>(&memory); }
Expand Down

0 comments on commit d7ca0bd

Please sign in to comment.