[PATCH] libstdc++: Enable dangling checks in std::pair and std::tuple [PR108822]

Tomasz Kaminski tkaminsk@redhat.com
Fri Sep 25 16:39:34 GMT 2026


On Fri, Sep 25, 2026 at 6:26 PM Jonathan Wakely <jwakely@redhat.com> wrote:

> 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.
> ---
>
> Tested x86_64-linux.
>
I think this is good time in release cycle to do that, and collect
feedback.

>
>  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), \
> --
> 2.55.0
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20260925/b694b9a4/attachment.htm>


More information about the Libstdc++ mailing list