[libcxx-commits] [libcxx] [libc++] Implement the `indirect` half of P3019R14: Vocabulary Types for Composite Class Design (PR #166717)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 6 08:18:02 PST 2025


================
@@ -0,0 +1,340 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___MEMORY_INDIRECT_H
+#define _LIBCPP___MEMORY_INDIRECT_H
+
+#include <__config>
+
+#include <__assert>
+#include <__compare/strong_order.h>
+#include <__compare/synth_three_way.h>
+#include <__functional/hash.h>
+#include <__fwd/memory_resource.h>
+#include <__memory/addressof.h>
+#include <__memory/allocation_guard.h>
+#include <__memory/allocator_arg_t.h>
+#include <__memory/allocator_traits.h>
+#include <__memory/swap_allocator.h>
+#include <__type_traits/is_array.h>
+#include <__type_traits/is_assignable.h>
+#include <__type_traits/is_constructible.h>
+#include <__type_traits/is_object.h>
+#include <__type_traits/is_same.h>
+#include <__type_traits/remove_cv.h>
+#include <__type_traits/remove_cvref.h>
+#include <__utility/exchange.h>
+#include <__utility/forward.h>
+#include <__utility/in_place.h>
+#include <__utility/move.h>
+#include <__utility/swap.h>
+#include <initializer_list>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 26
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _Tp, class _Allocator = allocator<_Tp>>
+class _LIBCPP_NO_SPECIALIZATIONS indirect {
+public:
+  using value_type     = _Tp;
+  using allocator_type = _Allocator;
+  using pointer        = allocator_traits<_Allocator>::pointer;
+  using const_pointer  = allocator_traits<_Allocator>::const_pointer;
+
+  static_assert(__check_valid_allocator<allocator_type>::value);
+  static_assert(is_same_v<typename allocator_type::value_type, value_type>);
+  static_assert(is_object_v<value_type>);
+  static_assert(!is_array_v<value_type>);
+  static_assert(!is_same_v<value_type, in_place_t>);
+  static_assert(!__is_inplace_type<value_type>::value);
+  static_assert(std::is_same_v<value_type, remove_cv_t<value_type>>,
+                "value_type must not be const or volatile qualified");
+
+  // [indirect.ctor], constructors
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit indirect()
+    requires is_default_constructible_v<_Allocator>
+      : __p_(__allocate_owned_object(__alloc_)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit indirect(allocator_arg_t, const _Allocator& __a)
+      : __alloc_(__a), __p_(__allocate_owned_object(__alloc_)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr indirect(const indirect& __other)
+      : __alloc_(allocator_traits<_Allocator>::select_on_container_copy_construction(__other.__alloc_)),
+        __p_(__other.valueless_after_move() ? nullptr : __allocate_owned_object(__alloc_, *__other)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr indirect(allocator_arg_t, const _Allocator& __a, const indirect& __other)
+      : __alloc_(__a), __p_(__other.valueless_after_move() ? nullptr : __allocate_owned_object(__alloc_, *__other)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr indirect(indirect&& __other) noexcept
+      : __alloc_(std::move(__other.__alloc_)), __p_(std::exchange(__other.__p_, nullptr)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr indirect(allocator_arg_t, const _Allocator& __a, indirect&& __other) noexcept
+    requires allocator_traits<_Allocator>::is_always_equal::value
+      : __alloc_(__a), __p_(std::exchange(__other.__p_, nullptr)) {}
+
+  _LIBCPP_HIDE_FROM_ABI constexpr indirect(allocator_arg_t, const _Allocator& __a, indirect&& __other) : __alloc_(__a) {
+    if (__other.valueless_after_move()) {
+      __p_ = nullptr;
+    } else if (__alloc_ == __other.__alloc_) {
+      __p_ = std::exchange(__other.__p_, nullptr);
+    } else {
+      __p_ = __allocate_owned_object(__alloc_, *std::move(__other));
+      __other.__destroy_owned_object();
+      __other.__p_ = nullptr;
+    }
+  }
+
+  template <class _Up = _Tp>
+    requires(!is_same_v<remove_cvref_t<_Up>, indirect> && !is_same_v<remove_cvref_t<_Up>, in_place_t> &&
+             is_constructible_v<_Tp, _Up> && is_default_constructible_v<_Allocator>)
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit indirect(_Up&& __u)
+      : __p_(__allocate_owned_object(__alloc_, std::forward<_Up>(__u))) {}
+
+  template <class _Up = _Tp>
+    requires(!is_same_v<remove_cvref_t<_Up>, indirect> && !is_same_v<remove_cvref_t<_Up>, in_place_t> &&
+             is_constructible_v<_Tp, _Up>)
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit indirect(allocator_arg_t, const _Allocator& __a, _Up&& __u)
+      : __alloc_(__a), __p_(__allocate_owned_object(__alloc_, std::forward<_Up>(__u))) {}
+
+  template <class... _Us>
+    requires(is_constructible_v<_Tp, _Us...> && is_default_constructible_v<_Allocator>)
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit indirect(in_place_t, _Us&&... __us)
+      : __p_(__allocate_owned_object(__alloc_, std::forward<_Us>(__us)...)) {}
+
+  template <class... _Us>
+    requires is_constructible_v<_Tp, _Us...>
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit indirect(allocator_arg_t, const _Allocator& __a, in_place_t, _Us&&... __us)
+      : __alloc_(__a), __p_(__allocate_owned_object(__alloc_, std::forward<_Us>(__us)...)) {}
+
+  template <class _In, class... _Us>
+    requires(is_constructible_v<_Tp, initializer_list<_In>&, _Us...> && is_default_constructible_v<_Allocator>)
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit indirect(in_place_t, initializer_list<_In> __ilist, _Us&&... __us)
+      : __p_(__allocate_owned_object(__alloc_, __ilist, std::forward<_Us>(__us)...)) {}
+
+  template <class _In, class... _Us>
+    requires is_constructible_v<_Tp, initializer_list<_In>&, _Us...>
+  _LIBCPP_HIDE_FROM_ABI constexpr explicit indirect(
+      allocator_arg_t, const _Allocator& __a, in_place_t, initializer_list<_In> __ilist, _Us&&... __us)
+      : __alloc_(__a), __p_(__allocate_owned_object(__alloc_, __ilist, std::forward<_Us>(__us)...)) {}
+
+  // [indirect.dtor], destructor
+  _LIBCPP_HIDE_FROM_ABI constexpr ~indirect() { __destroy_owned_object(); }
+
+  // [indirect.assign], assignment
+  _LIBCPP_HIDE_FROM_ABI constexpr indirect& operator=(const indirect& __other) {
+    if (std::addressof(__other) == this)
+      return *this;
+
+    static constexpr bool __propagate_allocator =
+        allocator_traits<_Allocator>::propagate_on_container_copy_assignment::value;
+    if (__other.valueless_after_move()) {
+      __destroy_owned_object();
+      __p_ = nullptr;
+    } else if (!valueless_after_move() && __alloc_ == __other.__alloc_) {
+      *__p_ = *__other;
+    } else {
+      pointer __new_p;
+      if constexpr (__propagate_allocator) {
+        // We need a mutable instance of the allocator, so make a copy.
+        _Allocator __alloc_copy = __other.__alloc_;
+        __new_p                 = __allocate_owned_object(__alloc_copy, *__other);
+      } else {
+        __new_p = __allocate_owned_object(__alloc_, *__other);
+      }
+      __destroy_owned_object();
+      __p_ = __new_p;
+    }
+
+    if constexpr (__propagate_allocator)
+      __alloc_ = __other.__alloc_;
+
+    return *this;
+  }
+
+  _LIBCPP_HIDE_FROM_ABI constexpr indirect& operator=(indirect&& __other) noexcept(
+      allocator_traits<_Allocator>::propagate_on_container_move_assignment::value ||
+      allocator_traits<_Allocator>::is_always_equal::value) {
+    if (std::addressof(__other) == this)
+      return *this;
+
+    static constexpr bool __propagate_allocator =
+        allocator_traits<_Allocator>::propagate_on_container_move_assignment::value;
+
+    pointer __new_p;
+    if constexpr (__propagate_allocator || allocator_traits<_Allocator>::is_always_equal::value) {
+      __new_p = __other.__p_;
+    } else if (__other.valueless_after_move()) {
+      __new_p = nullptr;
+    } else if (__alloc_ == __other.__alloc_) {
+      __new_p = __other.__p_;
+    } else {
+      __new_p = __allocate_owned_object(__alloc_, *std::move(__other));
+      __other.__destroy_owned_object();
+    }
+    __other.__p_ = nullptr;
+    __destroy_owned_object();
+    __p_ = __new_p;
+
+    if constexpr (__propagate_allocator)
+      __alloc_ = __other.__alloc_;
+
+    return *this;
+  }
+
+  template <class _Up = _Tp>
+    requires(!is_same_v<remove_cvref_t<_Up>, indirect> && is_constructible_v<_Tp, _Up> && is_assignable_v<_Tp&, _Up>)
+  _LIBCPP_HIDE_FROM_ABI constexpr indirect& operator=(_Up&& __u) {
+    if (valueless_after_move())
+      __p_ = __allocate_owned_object(__alloc_, std::forward<_Up>(__u));
+    else
+      *__p_ = std::forward<_Up>(__u);
+    return *this;
+  }
+
+  // [indirect.obs], observers
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
----------------
philnik777 wrote:

Please add tests for `[[nodiscard]]`.

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


More information about the libcxx-commits mailing list