# Deprecating llvm::Optional\<X\>::hasValue/getValue/getValueOr

**URL:** <https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716>\
**Category:** LLVM Project\
**Created:** [July 11, 2022, 1:41am UTC](https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716 "2022-07-11T01:41:04Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![MaskRay](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/maskray/32/81_2.png) [@MaskRay](https://discourse.llvm.org/u/MaskRay)\
**Post date:** [December 16, 2022, 10:20pm UTC](https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716/19 "2022-12-16T22:20:32Z")

</div>

`std::optional::value()` has undesired exception checking semantics and calls `__throw_bad_optional_access` in libc++. Moreover, the API is unavailable without \_LIBCPP\_NO\_EXCEPTIONS on older Mach-O platforms (e.g. macOS\<10.13), see `_LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS` in libcxx/.

At this point, the only practical approach is to migrate `value()` to `operator*` (and sometimes `operator->` or `value_or`). We’ll have to find previous `llvm::Optional::getValue` call sites like  
[611ffcf4e4a2ab19063174f6990969f96e9078de](https://github.com/llvm/llvm-project/commit/611ffcf4e4a2ab19063174f6990969f96e9078de) and change them to `operator*`.

My language server says there are 400+ references.

> … but that’s likely going to be a fairly annoying thing to fix going forward.

Hopefully not many people use `value()`. Many have learned to discourage `std::vector::at` in favor of `operator[]`.

I think `llvm::Optional::value` needs `LLVM_DEPRECATED` to discourage uses.

---

_[View the full topic](https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716)._
