[libc-commits] [PATCH] D129920: [libc] Trivial implementation of std::optional

Jeff Bailey via Phabricator via libc-commits libc-commits at lists.llvm.org
Sat Jul 16 10:24:24 PDT 2022

jeffbailey added a comment.

In D129920#3657048 <https://reviews.llvm.org/D129920#3657048>, @tschuett wrote:

> Do you need the union and the `Placeholder`? In `reset` you reset the `StoredValue`. This implies something about `T`.

I think I need the union because I don't want to construct the object if I'm not using it as in this case:

  std::optional<std::string> foo() {
    return std::nullopt;

Does a union still have that property if it only has a single member?  Or is there a different approach?

> I would add asserts to catch misbehaviour.

Will do.  It's almost tempting to also implement std::variant and use that since we'll probably want it later anyway.

> The LLVM optional uses a union for storage.

Right.  They're using the same approach that I am with:

      char __null_state_;
      value_type __val_;

I've just used the words Placeholder and StoredValue.  They're using __engaged where I'm using InUse.

  rG LLVM Github Monorepo



More information about the libc-commits mailing list