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.

From-SVN: r240929
This commit is contained in:
Jonathan Wakely 2016-10-10 16:34:28 +01:00 committed by Jonathan Wakely
parent 5f86926613
commit 9e023e3321
7 changed files with 82 additions and 10 deletions

View File

@ -1,5 +1,14 @@
2016-10-10 Jonathan Wakely <jwakely@redhat.com>
* 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.
* include/bits/allocator.h (allocator<T>::is_always_equal): Define.
* testsuite/20_util/allocator/requirements/typedefs.cc: Test for
is_always_equal.

View File

@ -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>

View File

@ -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>

View File

@ -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); }

View File

@ -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()" );
}

View File

@ -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>

View File

@ -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>