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

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 7 03:03:34 PDT 2026


https://github.com/frederick-vs-ja created https://github.com/llvm/llvm-project/pull/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.

>From f5620f924c700a14574af1394496033484853f3c Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Fri, 7 Aug 2026 17:59:43 +0800
Subject: [PATCH] [libc++] Include range access headers in `<optional>` only
 since C++26

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 increase the size of `<optional>` by ~8%, so
probably it is better to avoid such overhead in old modes.
---
 libcxx/include/optional | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

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



More information about the libcxx-commits mailing list