[libcxx-commits] [libcxx] [libc++] Implement P2988R12: `std::optional<T&>` (PR #155202)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Oct 26 20:52:47 PDT 2025
frederick-vs-ja wrote:
> Just to confirm, is the intended change to disallow `std::make_optional` for reference types?
No. The intended change is to make `std::make_optional<X&>(x)` always return an `optional<X&>` in C++26. Before C++26, such well-formed `std::make_optional<X&>(x)` possibly returned an `optional<decay_t<X>>`.
E.g. given
```C++
const int n = 42;
auto o = std::make_optional<const int&>(n);
```
`decltype(o)` is `optional<int>` until C++26, but `optional<const int&>` since C++26.
Since C++26 we need to add a barrier template parameter to the first `std::make_optional` overload. We can follow the pattern for `variant::visit` (which generally disallows users to accidently specify the template argument).
https://github.com/llvm/llvm-project/blob/57ba58d55843f3429e79f4086428a23dbf9375f6/libcxx/include/variant#L1306-L1313
https://github.com/llvm/llvm-project/pull/155202
More information about the libcxx-commits
mailing list