[libcxx-commits] [libcxx] [NFC][libc++] Adds (multi|)(map|set) forward declarations. (PR #131541)

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 19 10:15:26 PDT 2025


https://github.com/mordante updated https://github.com/llvm/llvm-project/pull/131541

>From 5ab4f47c0994cfb724fa65ceeebb38c680fef148 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Sun, 16 Mar 2025 21:08:35 +0100
Subject: [PATCH] [NFC][libc++] Adds (multi|)(map|set) forward declarations.

This removes duplicated forward declarations of these classes.

closes: #131518
---
 libcxx/include/CMakeLists.txt            |  2 ++
 libcxx/include/__functional/operations.h |  5 +---
 libcxx/include/__fwd/functional.h        |  7 ++++++
 libcxx/include/__fwd/map.h               | 31 ++++++++++++++++++++++++
 libcxx/include/__fwd/set.h               | 30 +++++++++++++++++++++++
 libcxx/include/__tree                    | 11 ++-------
 libcxx/include/map                       |  9 +++----
 libcxx/include/module.modulemap          |  9 +++++--
 libcxx/include/set                       |  6 ++---
 9 files changed, 85 insertions(+), 25 deletions(-)
 create mode 100644 libcxx/include/__fwd/map.h
 create mode 100644 libcxx/include/__fwd/set.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index d7c36d6b438fb..443e58fffe0d4 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -439,12 +439,14 @@ set(files
   __fwd/get.h
   __fwd/ios.h
   __fwd/istream.h
+  __fwd/map.h
   __fwd/mdspan.h
   __fwd/memory.h
   __fwd/memory_resource.h
   __fwd/ostream.h
   __fwd/pair.h
   __fwd/queue.h
+  __fwd/set.h
   __fwd/span.h
   __fwd/sstream.h
   __fwd/stack.h
diff --git a/libcxx/include/__functional/operations.h b/libcxx/include/__functional/operations.h
index 67d9da289aead..14357ef0a3ca5 100644
--- a/libcxx/include/__functional/operations.h
+++ b/libcxx/include/__functional/operations.h
@@ -13,6 +13,7 @@
 #include <__config>
 #include <__functional/binary_function.h>
 #include <__functional/unary_function.h>
+#include <__fwd/functional.h>
 #include <__type_traits/desugars_to.h>
 #include <__type_traits/is_integral.h>
 #include <__utility/forward.h>
@@ -349,11 +350,7 @@ struct _LIBCPP_TEMPLATE_VIS not_equal_to<void> {
 };
 #endif
 
-#if _LIBCPP_STD_VER >= 14
-template <class _Tp = void>
-#else
 template <class _Tp>
-#endif
 struct _LIBCPP_TEMPLATE_VIS less : __binary_function<_Tp, _Tp, bool> {
   typedef bool __result_type; // used by valarray
   _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {
diff --git a/libcxx/include/__fwd/functional.h b/libcxx/include/__fwd/functional.h
index 32c9ef33e453b..343a8dc22b8d3 100644
--- a/libcxx/include/__fwd/functional.h
+++ b/libcxx/include/__fwd/functional.h
@@ -17,6 +17,13 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+#if _LIBCPP_STD_VER >= 14
+template <class _Tp = void>
+#else
+template <class _Tp>
+#endif
+struct _LIBCPP_TEMPLATE_VIS less;
+
 template <class>
 struct _LIBCPP_TEMPLATE_VIS hash;
 
diff --git a/libcxx/include/__fwd/map.h b/libcxx/include/__fwd/map.h
new file mode 100644
index 0000000000000..aad404ca12b8c
--- /dev/null
+++ b/libcxx/include/__fwd/map.h
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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___FWD_MAP_H
+#define _LIBCPP___FWD_MAP_H
+
+#include <__config>
+#include <__fwd/functional.h>
+#include <__fwd/memory.h>
+#include <__fwd/pair.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
+class _LIBCPP_TEMPLATE_VIS map;
+
+template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
+class _LIBCPP_TEMPLATE_VIS multimap;
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___FWD_MAP_H
diff --git a/libcxx/include/__fwd/set.h b/libcxx/include/__fwd/set.h
new file mode 100644
index 0000000000000..0ae2c4109c19d
--- /dev/null
+++ b/libcxx/include/__fwd/set.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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___FWD_SET_H
+#define _LIBCPP___FWD_SET_H
+
+#include <__config>
+#include <__fwd/functional.h>
+#include <__fwd/memory.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
+class _LIBCPP_TEMPLATE_VIS set;
+
+template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
+class _LIBCPP_TEMPLATE_VIS multiset;
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___FWD_SET_H
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index c627641d5d86f..08ae8996f8f7d 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -13,6 +13,8 @@
 #include <__algorithm/min.h>
 #include <__assert>
 #include <__config>
+#include <__fwd/map.h>
+#include <__fwd/set.h>
 #include <__iterator/distance.h>
 #include <__iterator/iterator_traits.h>
 #include <__iterator/next.h>
@@ -48,15 +50,6 @@ _LIBCPP_PUSH_MACROS
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class, class, class, class>
-class _LIBCPP_TEMPLATE_VIS map;
-template <class, class, class, class>
-class _LIBCPP_TEMPLATE_VIS multimap;
-template <class, class, class>
-class _LIBCPP_TEMPLATE_VIS set;
-template <class, class, class>
-class _LIBCPP_TEMPLATE_VIS multiset;
-
 template <class _Tp, class _Compare, class _Allocator>
 class __tree;
 template <class _Tp, class _NodePtr, class _DiffType>
diff --git a/libcxx/include/map b/libcxx/include/map
index 37a8ec91b8f1f..e7e0c14e36999 100644
--- a/libcxx/include/map
+++ b/libcxx/include/map
@@ -582,6 +582,7 @@ erase_if(multimap<Key, T, Compare, Allocator>& c, Predicate pred);  // C++20
 #  include <__functional/binary_function.h>
 #  include <__functional/is_transparent.h>
 #  include <__functional/operations.h>
+#  include <__fwd/map.h>
 #  include <__iterator/erase_if_container.h>
 #  include <__iterator/iterator_traits.h>
 #  include <__iterator/ranges_iterator_traits.h>
@@ -756,10 +757,6 @@ public:
   }
 };
 
-template <class _Key, class _Tp, class _Compare, class _Allocator>
-class map;
-template <class _Key, class _Tp, class _Compare, class _Allocator>
-class multimap;
 template <class _TreeIterator>
 class __map_const_iterator;
 
@@ -971,7 +968,7 @@ public:
   friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
 };
 
-template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
+template <class _Key, class _Tp, class _Compare, class _Allocator>
 class _LIBCPP_TEMPLATE_VIS map {
 public:
   // types:
@@ -1656,7 +1653,7 @@ struct __container_traits<map<_Key, _Tp, _Compare, _Allocator> > {
   static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
 };
 
-template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
+template <class _Key, class _Tp, class _Compare, class _Allocator>
 class _LIBCPP_TEMPLATE_VIS multimap {
 public:
   // types:
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index b9964dac84acd..99729b3c7b997 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -1382,7 +1382,10 @@ module std [system] {
       header "__functional/not_fn.h"
       export std.functional.perfect_forward // inherited from and using its operators
     }
-    module operations                   { header "__functional/operations.h" }
+    module operations {
+      header "__functional/operations.h"
+      export std_core.fwd.functional
+    }
     module perfect_forward {
       header "__functional/perfect_forward.h"
       export std.tuple
@@ -1548,6 +1551,7 @@ module std [system] {
   }
 
   module map {
+    module fwd { header "__fwd/map.h" }
     header "map"
     export *
   }
@@ -1905,6 +1909,7 @@ module std [system] {
   }
 
   module set {
+    module fwd { header "__fwd/set.h" }
     header "set"
     export *
   }
@@ -1972,7 +1977,7 @@ module std [system] {
       export std.utility.element_count // used as part of the constexpr C function's API
     }
     module extern_template_lists    { header "__string/extern_template_lists.h" }
-    module fwd                      {  header "__fwd/string.h" }
+    module fwd                      { header "__fwd/string.h" }
 
     header "string"
     export *
diff --git a/libcxx/include/set b/libcxx/include/set
index bd7bfef1f3e29..1f60dc7c45bd8 100644
--- a/libcxx/include/set
+++ b/libcxx/include/set
@@ -522,6 +522,7 @@ erase_if(multiset<Key, Compare, Allocator>& c, Predicate pred);  // C++20
 #  include <__config>
 #  include <__functional/is_transparent.h>
 #  include <__functional/operations.h>
+#  include <__fwd/set.h>
 #  include <__iterator/erase_if_container.h>
 #  include <__iterator/iterator_traits.h>
 #  include <__iterator/ranges_iterator_traits.h>
@@ -570,9 +571,6 @@ _LIBCPP_PUSH_MACROS
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Key, class _Compare, class _Allocator>
-class multiset;
-
-template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
 class _LIBCPP_TEMPLATE_VIS set {
 public:
   // types:
@@ -1034,7 +1032,7 @@ struct __container_traits<set<_Key, _Compare, _Allocator> > {
   static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
 };
 
-template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
+template <class _Key, class _Compare, class _Allocator>
 class _LIBCPP_TEMPLATE_VIS multiset {
 public:
   // types:



More information about the libcxx-commits mailing list