[libcxx-commits] [libcxx] [libcxx] implement LWG4148: unique_ptr::operator* should not allow dangling references (PR #128213)

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Fri Feb 21 11:08:06 PST 2025


================
@@ -261,7 +261,10 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
   }
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator*() const
-      _NOEXCEPT_(_NOEXCEPT_(*std::declval<pointer>())) {
+      _NOEXCEPT_(_NOEXCEPT_(*std::declval<pointer>()))
+    // TODO: use reference_converts_from_temporary_v once implemented.
+    requires(!__reference_converts_from_temporary(__add_lvalue_reference_t<_Tp>, decltype(*declval<pointer>())))
----------------
mordante wrote:

This is implemented as a constraint. That has several issues:
* it's only available in C++20 and newer, this code should work in C++11
* this is not always a mandates, a `static_assert` is the way to do this. Please add a useful message in the `static_assert`.

https://github.com/llvm/llvm-project/pull/128213


More information about the libcxx-commits mailing list