diff --git a/libcxx/include/__utility/pair.h b/libcxx/include/__utility/pair.h --- a/libcxx/include/__utility/pair.h +++ b/libcxx/include/__utility/pair.h @@ -168,18 +168,20 @@ is_nothrow_copy_constructible::value) : first(__t1), second(__t2) {} - template() - >::type* = nullptr> + template < + class _U1 = _T1, class _U2 = _T2, + typename enable_if<_CheckArgs::template __enable_explicit<_U1, _U2>()>::type* = nullptr + > _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit pair(_U1&& __u1, _U2&& __u2) _NOEXCEPT_((is_nothrow_constructible::value && is_nothrow_constructible::value)) : first(_VSTD::forward<_U1>(__u1)), second(_VSTD::forward<_U2>(__u2)) {} - template() - >::type* = nullptr> + template < + class _U1 = _T1, class _U2 = _T2, + typename enable_if<_CheckArgs::template __enable_implicit<_U1, _U2>()>::type* = nullptr + > _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 pair(_U1&& __u1, _U2&& __u2) _NOEXCEPT_((is_nothrow_constructible::value && diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/U_V.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/U_V.pass.cpp --- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/U_V.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/U_V.pass.cpp @@ -98,9 +98,8 @@ #endif // Test support for http://wg21.link/P1951, default arguments for pair's constructor. - // Basically, this turns copies for brace initialization into moves. Note that libc++ - // applies this in all standard modes. -#if TEST_STD_VER > 20 || defined(_LIBCPP_VERSION) + // Basically, this turns copies for brace initialization into moves. +#if TEST_STD_VER > 20 { struct TrackInit { TrackInit() = default; @@ -138,7 +137,7 @@ } } } -#endif +#endif // TEST_STD_VER > 20 return 0; } diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.brace-init.P1951.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.brace-init.P1951.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.brace-init.P1951.pass.cpp @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: c++11 || c++14 || c++17 || c++20 + +// This test makes sure that we don't apply P1951 before C++23, since that is +// a breaking change. The examples in this test are taken from Richard Smith's +// comments on https://llvm.org/D109066. + +#include +#include + +struct A { + int *p_; + A(int *p) : p_(p) { *p_ += 1; } + A(const A& a) : p_(a.p_) { *p_ += 1; } + ~A() { *p_ -= 1; } +}; + +int main(int, char**) { + // Example 1: + // Without P1951, we call the `pair(int, const A&)` constructor (the converting constructor is not usable because + // we can't deduce from an initializer list), which creates the A temporary as part of the call to f. With P1951, + // we call the `pair(U&&, V&&)` constructor, which creates a A temporary inside the pair constructor, and that + // temporary doesn't live long enough any more. + { + int i = 0; + auto f = [&](std::pair) { assert(i >= 1); }; + f({42, &i}); + } + + // Example 2: + // Here, n doesn't need to be captured if we call the `pair(const int&, const long&)` constructor, because + // the lvalue-to-rvalue conversion happens in the lambda. But if we call the `pair(U&&, V&&)` constructor + // (deducing V = int), then n does need to be captured. + { + const int n = 5; + (void) []{ std::pair({1}, n); }; + } + + return 0; +}