[libcxx-commits] [libcxx] 20667a6 - [libc++] Include range access headers in `<optional>` only since C++26 (#214688)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 7 18:49:04 PDT 2026


Author: A. Jiang
Date: 2026-08-08T09:48:59+08:00
New Revision: 20667a67e6a47351dcf29acc5b4fb68d580ed6cf

URL: https://github.com/llvm/llvm-project/commit/20667a67e6a47351dcf29acc5b4fb68d580ed6cf
DIFF: https://github.com/llvm/llvm-project/commit/20667a67e6a47351dcf29acc5b4fb68d580ed6cf.diff

LOG: [libc++] Include range access headers in `<optional>` only since C++26 (#214688)

Inclusions of internal range access headers address LWG4131 which
patches P3168R2 "Give `std::optional` Range Support". As P3168R2 is
certainly a new feature in C++26, these includes are unnecessary in old
modes.

It was reported that these increased the size of `<optional>` by ~8%, so
probably it is better to avoid such cost in old modes.

Added: 
    

Modified: 
    libcxx/include/optional
    libcxx/test/std/iterators/iterator.range/mandatory_inclusions.gen.py

Removed: 
    


################################################################################
diff  --git a/libcxx/include/optional b/libcxx/include/optional
index bb12247b69083..0d0409ee8e2b3 100644
--- a/libcxx/include/optional
+++ b/libcxx/include/optional
@@ -275,12 +275,14 @@ namespace std {
 // [optional.syn]
 #  include <compare>
 
+#  if _LIBCPP_STD_VER >= 26
 // [iterator.range]
-#  include <__iterator/access.h>
-#  include <__iterator/data.h>
-#  include <__iterator/empty.h>
-#  include <__iterator/reverse_access.h>
-#  include <__iterator/size.h>
+#    include <__iterator/access.h>
+#    include <__iterator/data.h>
+#    include <__iterator/empty.h>
+#    include <__iterator/reverse_access.h>
+#    include <__iterator/size.h>
+#  endif // _LIBCPP_STD_VER >= 26
 
 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #    pragma GCC system_header

diff  --git a/libcxx/test/std/iterators/iterator.range/mandatory_inclusions.gen.py b/libcxx/test/std/iterators/iterator.range/mandatory_inclusions.gen.py
index 5d960b3bfb851..e1e93c4b9e0b6 100644
--- a/libcxx/test/std/iterators/iterator.range/mandatory_inclusions.gen.py
+++ b/libcxx/test/std/iterators/iterator.range/mandatory_inclusions.gen.py
@@ -51,6 +51,14 @@
     )
 )
 
+
+def get_standard_mode_threshold(header):
+    if header == "optional":
+        return "26"  # `optional` is a range only since C++26 via P3168R2.
+    else:
+        return "0"  # Other related components are ranges at first.
+
+
 for header in headers:
     print(
         f"""\
@@ -81,6 +89,7 @@
 }};
 
 int main(int, char**)  {{
+#if TEST_STD_VER >= {get_standard_mode_threshold(header)}
   {{
     Container c;
     const auto& cc = c;
@@ -174,6 +183,7 @@
     assert(std::ssize(cil) == 3);
 #endif
   }}
+#endif
 
   return 0;
 }}


        


More information about the libcxx-commits mailing list