Re: [PATCH] libstdc++: Make ranges::construct_at constexpr-friendly [PR95788]

2020-10-08 Thread Jonathan Wakely via Gcc-patches

On 07/10/20 12:10 -0400, Patrick Palka via Libstdc++ wrote:

On Wed, 30 Sep 2020, Patrick Palka wrote:


This rewrites ranges::construct_at in terms of std::construct_at so
that we can piggy back on the compiler's existing support for
recognizing placement new within std::construct_at during constexpr
evaluation instead of having to additionally teach the compiler about
ranges::construct_at.

While we're here, we should also make ranges::construct_at conditionally
noexcept like std::construct_at.

Tested on x86_64-pc-linux-gnu, does this look OK for trunk?

libstdc++-v3/ChangeLog:

PR libstdc++/95788
* include/bits/ranges_uninitialized.h:
(__construct_at_fn::operator()): Just call std::construct_at.
Declare it conditionally noexcept.
* testsuite/20_util/specialized_algorithms/construct_at/95788.cc:
New test.


Here's an updated patch that std::-qualifies the calls to declval in its
function signature and adjusts formatting accordingly:


OK for trunk and gcc-10, thanks.



Re: [PATCH] libstdc++: Make ranges::construct_at constexpr-friendly [PR95788]

2020-10-07 Thread Patrick Palka via Gcc-patches
On Wed, 30 Sep 2020, Patrick Palka wrote:

> This rewrites ranges::construct_at in terms of std::construct_at so
> that we can piggy back on the compiler's existing support for
> recognizing placement new within std::construct_at during constexpr
> evaluation instead of having to additionally teach the compiler about
> ranges::construct_at.
> 
> While we're here, we should also make ranges::construct_at conditionally
> noexcept like std::construct_at.
> 
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
> 
> libstdc++-v3/ChangeLog:
> 
>   PR libstdc++/95788
>   * include/bits/ranges_uninitialized.h:
>   (__construct_at_fn::operator()): Just call std::construct_at.
>   Declare it conditionally noexcept.
>   * testsuite/20_util/specialized_algorithms/construct_at/95788.cc:
>   New test.

Here's an updated patch that std::-qualifies the calls to declval in its
function signature and adjusts formatting accordingly:

-- >8 --

Subject: [PATCH] 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.
---
 .../include/bits/ranges_uninitialized.h   | 10 +++--
 .../construct_at/95788.cc | 41 +++
 2 files changed, 48 insertions(+), 3 deletions(-)
 create mode 100644 
libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/95788.cc

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
-  requires requires { ::new (declval()) _Tp(declval<_Args>()...); }
+  requires requires {
+   ::new (std::declval()) _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 000..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 
+
+constexpr bool
+test01()
+{
+  const int sz{1};
+  int* data{std::allocator{}.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{}.deallocate(data, sz);
+  return true;
+}
+
+static_assert(test01());
+
+struct S { S(); };
+S* p;
+static_assert(!noexcept(std::ranges::construct_at(p)));
-- 
2.29.0.rc0



[PATCH] libstdc++: Make ranges::construct_at constexpr-friendly [PR95788]

2020-09-30 Thread Patrick Palka via Gcc-patches
This rewrites ranges::construct_at in terms of std::construct_at so
that we can piggy back on the compiler's existing support for
recognizing placement new within std::construct_at during constexpr
evaluation instead of having to additionally teach the compiler about
ranges::construct_at.

While we're here, we should also make ranges::construct_at conditionally
noexcept like std::construct_at.

Tested on x86_64-pc-linux-gnu, does this look OK for trunk?

libstdc++-v3/ChangeLog:

PR libstdc++/95788
* include/bits/ranges_uninitialized.h:
(__construct_at_fn::operator()): Just call std::construct_at.
Declare it conditionally noexcept.
* testsuite/20_util/specialized_algorithms/construct_at/95788.cc:
New test.
---
 .../include/bits/ranges_uninitialized.h   |  6 +--
 .../construct_at/95788.cc | 40 +++
 2 files changed, 42 insertions(+), 4 deletions(-)
 create mode 100644 
libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/95788.cc

diff --git a/libstdc++-v3/include/bits/ranges_uninitialized.h 
b/libstdc++-v3/include/bits/ranges_uninitialized.h
index d758078fc03..def086508fb 100644
--- a/libstdc++-v3/include/bits/ranges_uninitialized.h
+++ b/libstdc++-v3/include/bits/ranges_uninitialized.h
@@ -496,10 +496,8 @@ namespace ranges
   requires requires { ::new (declval()) _Tp(declval<_Args>()...); }
   constexpr _Tp*
   operator()(_Tp* __location, _Args&&... __args) const
-  {
-   return ::new (__detail::__voidify(*__location))
-_Tp(std::forward<_Args>(__args)...);
-  }
+  noexcept(noexcept(std::construct_at(__location, declval<_Args>()...)))
+  { return std::construct_at(__location, std::forward<_Args>(__args)...); }
   };
 
   inline constexpr __construct_at_fn construct_at{};
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 000..aeb04de1ea3
--- /dev/null
+++ 
b/libstdc++-v3/testsuite/20_util/specialized_algorithms/construct_at/95788.cc
@@ -0,0 +1,40 @@
+// 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
+// .
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+#include 
+
+constexpr bool test01()
+{
+  const int sz{1};
+  int* data{std::allocator{}.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{}.deallocate(data, sz);
+  return true;
+}
+
+static_assert(test01());
+
+struct S { S(); };
+S *p;
+static_assert(!noexcept(std::ranges::construct_at(p)));
-- 
2.28.0.651.g306ee63a70