public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Implement constexpr std::addressof for C++17
@ 2016-10-10 15:34 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2016-10-10 15:34 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 543 bytes --]

Thank to the new __builtin_addressof that Jakub added we can do this
now.

	* doc/xml/manual/intro.xml: Document DR 2296 status.
	* doc/xml/manual/status_cxx2017.xml: Update status.
	* include/bits/move.h (__addressof): Add _GLIBCXX_CONSTEXPR and
	call __builtin_addressof.
	(addressof): Add _GLIBCXX17_CONSTEXPR.
	* testsuite/20_util/addressof/requirements/constexpr.cc: New test.
	* testsuite/20_util/forward/c_neg.cc: Adjust dg-error lineno.
	* testsuite/20_util/forward/f_neg.cc: Likewise.

Tested powerpc64le-linux, committed to trunk.



[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 6798 bytes --]

commit d450a86bff30b38464e440f6157c39b399b54cc1
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Oct 6 18:53:28 2016 +0100

    Implement constexpr std::addressof for C++17
    
    	* doc/xml/manual/intro.xml: Document DR 2296 status.
    	* doc/xml/manual/status_cxx2017.xml: Update status.
    	* include/bits/move.h (__addressof): Add _GLIBCXX_CONSTEXPR and
    	call __builtin_addressof.
    	(addressof): Add _GLIBCXX17_CONSTEXPR.
    	* testsuite/20_util/addressof/requirements/constexpr.cc: New test.
    	* testsuite/20_util/forward/c_neg.cc: Adjust dg-error lineno.
    	* testsuite/20_util/forward/f_neg.cc: Likewise.

diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml
index 4747851..265ef67 100644
--- a/libstdc++-v3/doc/xml/manual/intro.xml
+++ b/libstdc++-v3/doc/xml/manual/intro.xml
@@ -961,6 +961,13 @@ requirements of the license of GCC.
     is included by <code>&lt;array&gt;</code>.
     </para></listitem></varlistentry>
 
+    <varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../ext/lwg-defects.html#2296">2296</link>:
+	<emphasis><code>std::addressof</code> should be constexpr</emphasis>
+    </term>
+    <listitem><para>Use <code>__builtin_addressof</code> and add
+    <code>constexpr</code> to <code>addressof</code> for C++17 and later.
+    </para></listitem></varlistentry>
+
     <varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../ext/lwg-defects.html#2313">2313</link>:
 	<emphasis><code>tuple_size</code> should always derive from <code>integral_constant&lt;size_t, N&gt;</code></emphasis>
     </term>
diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml
index c03978e..c6b8440 100644
--- a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml
+++ b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml
@@ -253,14 +253,13 @@ Feature-testing recommendations for C++</link>.
     </row>
 
     <row>
-      <?dbhtml bgcolor="#C8B0B0" ?>
       <entry> <code>std::addressof</code> should be constexpr </entry>
       <entry>
 	<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0304r0.html#2296">
 	LWG2296
 	</link>
       </entry>
-      <entry align="center"> No </entry>
+      <entry align="center"> 7 </entry>
       <entry><code> __cpp_lib_addressof_constexpr >= 201603 </code></entry>
     </row>
 
diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h
index 9deec42..a5002fc 100644
--- a/libstdc++-v3/include/bits/move.h
+++ b/libstdc++-v3/include/bits/move.h
@@ -43,12 +43,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  @ingroup utilities
    */
   template<typename _Tp>
-    inline _Tp*
+    inline _GLIBCXX_CONSTEXPR _Tp*
     __addressof(_Tp& __r) _GLIBCXX_NOEXCEPT
-    {
-      return reinterpret_cast<_Tp*>
-	(&const_cast<char&>(reinterpret_cast<const volatile char&>(__r)));
-    }
+    { return __builtin_addressof(__r); }
 
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace
@@ -123,6 +120,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   // declval, from type_traits.
 
+#if __cplusplus > 201402L
+  // _GLIBCXX_RESOLVE_LIB_DEFECTS
+  // 2296. std::addressof should be constexpr
+# define __cpp_lib_addressof_constexpr 201603
+#endif
   /**
    *  @brief Returns the actual address of the object or function
    *         referenced by r, even in the presence of an overloaded
@@ -131,7 +133,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  @return   The actual address.
   */
   template<typename _Tp>
-    inline _Tp*
+    inline _GLIBCXX17_CONSTEXPR _Tp*
     addressof(_Tp& __r) noexcept
     { return std::__addressof(__r); }
 
diff --git a/libstdc++-v3/testsuite/20_util/addressof/requirements/constexpr.cc b/libstdc++-v3/testsuite/20_util/addressof/requirements/constexpr.cc
new file mode 100644
index 0000000..998d087
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/addressof/requirements/constexpr.cc
@@ -0,0 +1,55 @@
+// Copyright (C) 2016 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++1z" }
+// { dg-do compile { target c++1z } }
+
+#include <utility>
+
+// LWG 2296 std::addressof should be constexpr
+
+#ifndef __cpp_lib_addressof_constexpr
+# error "Feature-test macro for constexpr addressof missing"
+#elif __cpp_lib_addressof_constexpr != 201603
+# error "Feature-test macro for constexpr addressof has wrong value"
+#endif
+
+int i;
+constexpr int* pi = std::addressof(i);
+static_assert( pi == &i, "&i" );
+
+struct X {
+  void operator&();
+  constexpr X* addr() { return this; }
+  constexpr const X* addr() const { return this; }
+};
+X x;
+constexpr X* px = std::addressof(x);
+static_assert( px == x.addr(), "x.addr()" );
+const X cx;
+constexpr const X* pcx = std::addressof(cx);
+static_assert( pcx == cx.addr(), "cx.addr()" );
+
+void
+test01()
+{
+  static int i2;
+  static_assert( std::addressof(i2) == &i2, "&i2" );
+
+  static X x2;
+  static_assert( std::addressof(x2) == x2.addr(), "x2.addr()" );
+}
diff --git a/libstdc++-v3/testsuite/20_util/forward/c_neg.cc b/libstdc++-v3/testsuite/20_util/forward/c_neg.cc
index 0b23625..784e23c 100644
--- a/libstdc++-v3/testsuite/20_util/forward/c_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/forward/c_neg.cc
@@ -17,7 +17,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-error "static assertion failed" "" { target *-*-* } 89 }
+// { dg-error "static assertion failed" "" { target *-*-* } 86 }
 
 #include <list>
 
diff --git a/libstdc++-v3/testsuite/20_util/forward/f_neg.cc b/libstdc++-v3/testsuite/20_util/forward/f_neg.cc
index 9796ef4..3f4c175 100644
--- a/libstdc++-v3/testsuite/20_util/forward/f_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/forward/f_neg.cc
@@ -17,7 +17,7 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-error "static assertion failed" "" { target *-*-* } 89 }
+// { dg-error "static assertion failed" "" { target *-*-* } 86 }
 
 #include <utility>
 

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2016-10-10 15:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-10 15:34 [PATCH] Implement constexpr std::addressof for C++17 Jonathan Wakely

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).