On Tue, 2 Sep 2025, Jonathan Wakely wrote:
> For the pair(T1, T2) explicit deduction type to decay its arguments as
> intended, we need the pair(const T1&, const T2&) constructor to not be
> used for CTAD. Otherwise we try to instantiate pair<T1, T2> without
> decaying, which is ill-formed for function lvalues.
>
> Use std::type_identity_t<T1> to make the constructor unusable for an
> implicit deduction guide.
Makes sense, IIUC the pair(T1, T2) guide will always be preferred over
the implicit pair(const T1&, const T2&) guide, so we might as well
disable the latter. The fewer implicit deduction guides to consider,
the better!
(My understanding of the PR is that this is ultimately an implementation
bug rather than a specification bug, but this seems like a nice way to
sidestep the issue while making CTAD easier to reason about.)
I think tuple has the same issue..
>
> libstdc++-v3/ChangeLog:
>
> PR libstdc++/110853
> * include/bits/stl_pair.h [C++20] (pair(const T1&, const T2&)):
> Use std::type_identity_t<T1> for first parameter.
> * testsuite/20_util/pair/cons/110853.cc: New test.
> ---
>
> Lightly tested so far on x86_64-linux, full tests running overnight.
>
> This should be backported to all active branches.
>
> libstdc++-v3/include/bits/stl_pair.h | 2 +-
> libstdc++-v3/testsuite/20_util/pair/cons/110853.cc | 10 ++++++++++
> 2 files changed, 11 insertions(+), 1 deletion(-)
> create mode 100644 libstdc++-v3/testsuite/20_util/pair/cons/110853.cc
>
> diff --git a/libstdc++-v3/include/bits/stl_pair.h
> b/libstdc++-v3/include/bits/stl_pair.h
> index 661335b466a3..231d0bbd1f49 100644
> --- a/libstdc++-v3/include/bits/stl_pair.h
> +++ b/libstdc++-v3/include/bits/stl_pair.h
> @@ -445,7 +445,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>
> /// Constructor accepting lvalues of `first_type` and `second_type`
> constexpr explicit(!_S_convertible<const _T1&, const _T2&>())
> - pair(const _T1& __x, const _T2& __y)
> + pair(const type_identity_t<_T1>& __x, const _T2& __y)
> noexcept(_S_nothrow_constructible<const _T1&, const _T2&>())
> requires (_S_constructible<const _T1&, const _T2&>())
> : first(__x), second(__y)
> diff --git a/libstdc++-v3/testsuite/20_util/pair/cons/110853.cc
> b/libstdc++-v3/testsuite/20_util/pair/cons/110853.cc
> new file mode 100644
> index 000000000000..1047590686d1
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/20_util/pair/cons/110853.cc
> @@ -0,0 +1,10 @@
> +// { dg-do compile { target c++17 } }
> +// PR libstdc++/110853
> +// Bad interaction between deduction guide with decay and constraints
> +// (CTAD, std::pair and function lvalue)
> +
> +#include <utility>
> +
> +void func() {}
> +std::pair p(1, value_twice);
> +std::pair<int, void (*)()>& r = p;
> --
> 2.51.0
>
>