[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,332 @@
+// -*- 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 <__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_object.h>
+#include <__type_traits/is_same.h>
+#include <__type_traits/remove_cv.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>
+#include <type_traits>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+#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)) {}
----------------
philnik777 wrote:

IMO we should keep them as one. Constructor overload sets are already way too big, let's not make it worse unnecessarily.

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


More information about the libcxx-commits mailing list