[gcc r17-4727] libstdc++: Enable dangling checks in std::pair and std::tuple [PR108822]
Jonathan Wakely
redi@gcc.gnu.org
Mon Sep 28 23:16:50 GMT 2026
https://gcc.gnu.org/g:98847dc641b95a29fe470b90e4fec034daa4300b
commit r17-4727-g98847dc641b95a29fe470b90e4fec034daa4300b
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Fri Sep 25 10:00:35 2026 +0100
libstdc++: Enable dangling checks in std::pair and std::tuple [PR108822]
In r13-6084-g916ce577ad109b I enabled the dangling-reference checks in
std::pair unconditionally for C++20 (which was experimental at the time)
but conditional on _GLIBCXX_DEBUG for C++17 and lower, saying:
The pre-C++17 assertions are only enabled for Debug Mode, to avoid
introducing a breaking change in Stage 4. We should consider enabling
them by default in Stage 1 for GCC 14.
In r14-7225-gf8a5298c97c460 I added the checks to std::tuple, again
making them conditional on _GLIBCXX_DEBUG for C++17 and lower. We never
revisited that during Stage 1 to remove the _GLIBCXX_DEBUG condition.
I'm not aware of problems caused by making it ill-formed to create pairs
and tuples with dangling references, so this commit removes the
_GLIBCXX_DEBUG condition. This makes it ill-formed to create a std::pair
or std::tuple that would have guaranteed UB if its element was accessed
at runtime. Programs which use such types in code which is never
executed should either remove the code, or use std::declval in
unevaluated contexts, to avoid instantiating the ill-formed
constructors.
libstdc++-v3/ChangeLog:
PR libstdc++/108822
* include/bits/stl_pair.h (__glibcxx_no_dangling_refs): Enable
for normal mode, not only Debug Mode.
* include/std/tuple (__glibcxx_no_dangling_refs): Likewise.
Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
Diff:
---
libstdc++-v3/include/bits/stl_pair.h | 3 +--
libstdc++-v3/include/std/tuple | 3 +--
2 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h
index f08b2714a4fe..ccff86912067 100644
--- a/libstdc++-v3/include/bits/stl_pair.h
+++ b/libstdc++-v3/include/bits/stl_pair.h
@@ -726,8 +726,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// @cond undocumented
// Error if construction from _U1 and _U2 would create a dangling ref.
-#if __has_builtin(__reference_constructs_from_temporary) \
- && defined _GLIBCXX_DEBUG
+#if __has_builtin(__reference_constructs_from_temporary)
# define __glibcxx_no_dangling_refs(_U1, _U2) \
static_assert(!__reference_constructs_from_temporary(_T1, _U1) \
&& !__reference_constructs_from_temporary(_T2, _U2), \
diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple
index 63b4010c0b5e..d73cefcbca87 100644
--- a/libstdc++-v3/include/std/tuple
+++ b/libstdc++-v3/include/std/tuple
@@ -2096,8 +2096,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// @cond undocumented
#undef __glibcxx_no_dangling_refs
// Error if construction from _U1 and _U2 would create a dangling ref.
-#if __has_builtin(__reference_constructs_from_temporary) \
- && defined _GLIBCXX_DEBUG
+#if __has_builtin(__reference_constructs_from_temporary)
# define __glibcxx_no_dangling_refs(_U1, _U2) \
static_assert(!__reference_constructs_from_temporary(_T1, _U1) \
&& !__reference_constructs_from_temporary(_T2, _U2), \
More information about the Libstdc++-cvs
mailing list