[llvm] [ADT] Add and use (for AArch64) `ValueWithSentinel<T, Sentinel>` (PR #158120)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 11 14:28:17 PDT 2025
https://github.com/kazutakahirata commented:
I'm a bit ambivalent about this PR. I like the safety aspect of `ValueWithSentinel`, but then I wonder if `ValueWithSentinel` makes code less readable than household names like `std::optional`.
How about naming the new class `OptionalWithSentinel` or something? This way, people might think of connections with `std::optional` in the back of their mind.
By the way, you might want to check to see if `CustomizableOptional` in `clang/Basic/CustomizableOptional.h` fits your bill. If so, feel free to move it back to `llvm/include/ADT`.
This class lets you define your own `OptionalStorage` class. Your `OptionalStorage` is responsible for constructors and `has_value`. IIUC, `CustomizableOptional<DirectoryEntryRef>` uses a trick similar to `std::numeric_limits<T>::max()` to avoid allocating a separate boolean value. `CustomizableOptional` handles the rest (`operator*`, `operator==`, etc). A quick history is that we used to have our own implementation of `std::optional` called `llvm::Optional`. `llvm::Optional` had this customizable storage capability. Since `clang` was the only user of the customizable storage, we moved it to `clang/include/clang/Basic` while the rest of the codebase uses the standard `std::optional`.
https://github.com/llvm/llvm-project/pull/158120
More information about the llvm-commits
mailing list