[libcxx-commits] [libcxx] [libc++] Implement P2988R12: `std::optional<T&>` (PR #155202)
William Tran-Viet via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Oct 26 21:09:32 PDT 2025
smallp-o-p 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
Does that mean the reference implementation [https://godbolt.org/z/jb3eocK8j](beman::optional godbolt) is incorrect? It explicitly disallows such a use of `make_optional`, but at the same time, the draft standard mentions no such thing. I'm leaning towards your explanation but a clarification would be helpful.
https://github.com/llvm/llvm-project/pull/155202
More information about the libcxx-commits
mailing list