From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id D3EE3385C6E5; Fri, 12 Jan 2024 09:48:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D3EE3385C6E5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705052899; bh=oglqRmtXTwIM4sTjhzJ2m/bo/M8eqf9T9Vf9/r+mXXs=; h=From:To:Subject:Date:From; b=Q/6MXMqwisaHH32yto4zFsC7DKrsG7UjQjW1cwd28RnHXsuhSr+88Foqaw+4MZFQ1 AXKX+fNwrjup3dfwzUUWAsqh2kSN07ifEN66ZX2SkHEqOgJChPRNVrhmXa3NHSTAaV JiYM81fhc10cbWRyONIanLkH7W3aVCUAwuVwNHXk= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-7173] libstdc++: Implement C++23 P1951R1 (Default Args for pair's Forwarding Ctor) [PR105505] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: e9035183a20af10a80a97b57accb76699c9c2530 X-Git-Newrev: 35f8c661d67f53486f2f3e25887323476a7d0c2b Message-Id: <20240112094819.D3EE3385C6E5@sourceware.org> Date: Fri, 12 Jan 2024 09:48:19 +0000 (GMT) List-Id: https://gcc.gnu.org/g:35f8c661d67f53486f2f3e25887323476a7d0c2b commit r14-7173-g35f8c661d67f53486f2f3e25887323476a7d0c2b Author: Jonathan Wakely Date: Thu Jan 11 22:38:02 2024 +0000 libstdc++: Implement C++23 P1951R1 (Default Args for pair's Forwarding Ctor) [PR105505] This was approved for C++23 at he June 2021 virtual meeting. This allows more efficient construction of std::pair members when {} is used as a constructor argument. The perfect forwarding constructor can be used, so that the member variables are constructed from forwarded arguments instead of being copy constructed from temporaries. libstdc++-v3/ChangeLog: PR libstdc++/105505 * include/bits/stl_pair.h (pair::pair(U1&&, U2&&)) [C++23]: Add default template arguments, as per P1951R1. * testsuite/20_util/pair/cons/default_tmpl_args.cc: New test. Diff: --- libstdc++-v3/include/bits/stl_pair.h | 8 ++++ .../20_util/pair/cons/default_tmpl_args.cc | 48 ++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h index ce8826ae6a8..52f532f3c39 100644 --- a/libstdc++-v3/include/bits/stl_pair.h +++ b/libstdc++-v3/include/bits/stl_pair.h @@ -308,7 +308,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { } /// Constructor accepting two values of arbitrary types +#if __cplusplus > 202002L + template +#else template +#endif requires (_S_constructible<_U1, _U2>()) && (!_S_dangles<_U1, _U2>()) constexpr explicit(!_S_convertible<_U1, _U2>()) pair(_U1&& __x, _U2&& __y) @@ -316,7 +320,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { } +#if __cplusplus > 202002L + template +#else template +#endif requires (_S_constructible<_U1, _U2>()) && (_S_dangles<_U1, _U2>()) constexpr explicit(!_S_convertible<_U1, _U2>()) pair(_U1&&, _U2&&) = delete; diff --git a/libstdc++-v3/testsuite/20_util/pair/cons/default_tmpl_args.cc b/libstdc++-v3/testsuite/20_util/pair/cons/default_tmpl_args.cc new file mode 100644 index 00000000000..5960bf72e21 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/pair/cons/default_tmpl_args.cc @@ -0,0 +1,48 @@ +// { dg-do compile { target c++23 } } + +// P1951R1 Default Arguments for pair's Forwarding Constructor + +#include +#include +#include +#include + +void +test_p1951r1_example() +{ + std::pair> p("hello", {}); +} + +struct Counter +{ + constexpr Counter() = default; + constexpr Counter(int) { } + constexpr Counter(const Counter& c) : copies(c.copies + 1), moves(c.moves) { } + constexpr Counter(Counter&& c) : copies(c.copies), moves(c.moves+1) { } + int copies = 0; + int moves = 0; +}; + +constexpr bool +test_count_copies() +{ + std::pair p1(1, {}); + VERIFY( p1.first.copies == 0 && p1.second.copies == 0 ); + VERIFY( p1.first.moves == 0 && p1.second.moves == 1 ); + + std::pair p2({}, 1); + VERIFY( p2.first.copies == 0 && p2.second.copies == 0 ); + VERIFY( p2.first.moves == 1 && p2.second.moves == 0 ); + + std::pair p3({}, {}); + VERIFY( p3.first.copies == 0 && p3.second.copies == 0 ); + VERIFY( p3.first.moves == 1 && p3.second.moves == 1 ); + + return true; +} + +int main() +{ + test_p1951r1_example(); + static_assert( test_count_copies() ); +}