public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Patrick Palka <ppalka@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r10-8888] libstdc++: Make ranges::construct_at constexpr-friendly [PR95788] Date: Tue, 13 Oct 2020 15:45:37 +0000 (GMT) [thread overview] Message-ID: <20201013154537.96A10383E809@sourceware.org> (raw) https://gcc.gnu.org/g:5b814b6b618c8b3f0ff71717b4929a6fdf950714 commit r10-8888-g5b814b6b618c8b3f0ff71717b4929a6fdf950714 Author: Patrick Palka <ppalka@redhat.com> Date: Thu Oct 8 18:10:05 2020 -0400 libstdc++: Make ranges::construct_at constexpr-friendly [PR95788] This rewrites ranges::construct_at in terms of std::construct_at so that we can piggyback on the compiler's existing support for intercepting placement new within std::construct_at during constexpr evaluation, instead of having to additionally teach the compiler about ranges::construct_at. While we're making changes to ranges::construct_at, this patch also declares it conditionally noexcept and qualifies the calls to declval in its requires-clause. libstdc++-v3/ChangeLog: PR libstdc++/95788 * include/bits/ranges_uninitialized.h: (__construct_at_fn::operator()): Rewrite in terms of std::construct_at. Declare it conditionally noexcept. Qualify calls to declval in its requires-clause. * testsuite/20_util/specialized_algorithms/construct_at/95788.cc: New test. (cherry picked from commit 9158a4d2a6cd58d6bb591d5ce64e766b399e4aef) Diff: --- libstdc++-v3/include/bits/ranges_uninitialized.h | 10 ++++-- .../specialized_algorithms/construct_at/95788.cc | 41 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_uninitialized.h b/libstdc++-v3/include/bits/ranges_uninitialized.h index d758078fc03..25e664de753 100644 --- a/libstdc++-v3/include/bits/ranges_uninitialized.h +++ b/libstdc++-v3/include/bits/ranges_uninitialized.h @@ -493,12 +493,16 @@ namespace ranges struct __construct_at_fn { template<typename _Tp, typename... _Args> - requires requires { ::new (declval<void*>()) _Tp(declval<_Args>()...); } + requires requires { + ::new (std::declval<void*>()) _Tp(std::declval<_Args>()...); + } constexpr _Tp* operator()(_Tp* __location, _Args&&... __args) const + noexcept(noexcept(std::construct_at(__location, + std::forward<_Args>(__args)...))) { - return ::new (__detail::__voidify(*__location)) - _Tp(std::forward<_Args>(__args)...); + return std::construct_at(__location, + std::forward<_Args>(__args)...); } }; diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/95788.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/95788.cc new file mode 100644 index 00000000000..895332b302b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/95788.cc @@ -0,0 +1,41 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include <memory> + +constexpr bool +test01() +{ + const int sz{1}; + int* data{std::allocator<int>{}.allocate(sz)}; + static_assert(noexcept(std::ranges::construct_at(data, 42))); + std::ranges::construct_at(data, 42); + if (*data != 42) + return false; + std::ranges::destroy_at(data); + std::allocator<int>{}.deallocate(data, sz); + return true; +} + +static_assert(test01()); + +struct S { S(); }; +S* p; +static_assert(!noexcept(std::ranges::construct_at(p)));
reply other threads:[~2020-10-13 15:45 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20201013154537.96A10383E809@sourceware.org \ --to=ppalka@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).