Add std::apply for C++17

* doc/xml/manual/status_cxx2017.xml: Add missing LFTSv2 features.
	* doc/html/manual/status.html: Regenerate.
	* include/Makefile.am: Add new header.
	* include/Makefile.in: Regenerate.
	* include/bits/invoke.h: New header.
	(__invoke): Make constexpr. Add && to types in exception specification.
	* include/experimental/tuple (apply, __apply_impl): Fix non-reserved
	names. Include <bits/invoke.h> and use std::__invoke.
	* include/std/functional (__invfwd, __invoke_impl, __invoke): Move to
	new header.
	(invoke): Add && to types in exception specification.
	* include/std/tuple (apply, __apply_impl): Define for C++17.
	* testsuite/20_util/tuple/apply/1.cc: New test.
	* testsuite/20_util/tuple/element_access/get_neg.cc: Adjust dg-error
	lineno.

From-SVN: r239183
This commit is contained in:
Jonathan Wakely 2016-08-06 01:06:02 +01:00 committed by Jonathan Wakely
parent 5c3e5002db
commit 3e9f67e6a2
11 changed files with 271 additions and 60 deletions

View File

@ -1,3 +1,21 @@
2016-08-06 Jonathan Wakely <jwakely@redhat.com>
* doc/xml/manual/status_cxx2017.xml: Add missing LFTSv2 features.
* doc/html/manual/status.html: Regenerate.
* include/Makefile.am: Add new header.
* include/Makefile.in: Regenerate.
* include/bits/invoke.h: New header.
(__invoke): Make constexpr. Add && to types in exception specification.
* include/experimental/tuple (apply, __apply_impl): Fix non-reserved
names. Include <bits/invoke.h> and use std::__invoke.
* include/std/functional (__invfwd, __invoke_impl, __invoke): Move to
new header.
(invoke): Add && to types in exception specification.
* include/std/tuple (apply, __apply_impl): Define for C++17.
* testsuite/20_util/tuple/apply/1.cc: New test.
* testsuite/20_util/tuple/element_access/get_neg.cc: Adjust dg-error
lineno.
2016-08-05 Jonathan Wakely <jwakely@redhat.com>
* doc/xml/manual/status_cxx2017.xml: Correct shared_from_this status.

View File

@ -515,7 +515,19 @@ Feature-testing recommendations for C++</a>.
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html" target="_top">
P0220R1
</a>
</td><td align="center"> No </td><td align="left"> <code class="code">__has_include(&lt;memory_resource&gt;)</code> </td></tr><tr><td align="left"> Constant View: A proposal for a <code class="code">std::as_const</code> helper function template </td><td align="left">
</td><td align="center"> No </td><td align="left"> <code class="code">__has_include(&lt;memory_resource&gt;)</code> </td></tr><tr><td align="left"> Library Fundamentals V1 TS Components: <code class="code">apply</code> </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html" target="_top">
P0220R1
</a>
</td><td align="center"> 7 </td><td align="left"> <code class="code">__cpp_lib_apply &gt;= 201603</code> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Library Fundamentals V1 TS Components: <code class="code">shared_ptr&lt;T[]&gt;</code> </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html" target="_top">
P0220R1
</a>
</td><td align="center"> No </td><td align="left"> <code class="code">__cpp_lib_shared_ptr_arrays &gt;= 201603</code> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Library Fundamentals V1 TS Components: Searchers </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html" target="_top">
P0220R1
</a>
</td><td align="center"> No </td><td align="left"> <code class="code">__cpp_lib_boyer_moore_searcher &gt;= 201603</code> </td></tr><tr><td align="left"> Constant View: A proposal for a <code class="code">std::as_const</code> helper function template </td><td align="left">
<a class="link" href="" target="_top">
P0007R1
</a>

View File

@ -148,6 +148,41 @@ Feature-testing recommendations for C++</link>.
<entry> <code>__has_include(&lt;memory_resource&gt;)</code> </entry>
</row>
<row>
<entry> Library Fundamentals V1 TS Components: <code>apply</code> </entry>
<entry>
<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html">
P0220R1
</link>
</entry>
<entry align="center"> 7 </entry>
<entry> <code>__cpp_lib_apply >= 201603</code> </entry>
</row>
<row>
<?dbhtml bgcolor="#C8B0B0" ?>
<entry> Library Fundamentals V1 TS Components: <code>shared_ptr&lt;T[]&gt;</code> </entry>
<entry>
<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html">
P0220R1
</link>
</entry>
<entry align="center"> No </entry>
<entry> <code>__cpp_lib_shared_ptr_arrays >= 201603</code> </entry>
</row>
<row>
<?dbhtml bgcolor="#C8B0B0" ?>
<entry> Library Fundamentals V1 TS Components: Searchers </entry>
<entry>
<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html">
P0220R1
</link>
</entry>
<entry align="center"> No </entry>
<entry> <code>__cpp_lib_boyer_moore_searcher >= 201603</code> </entry>
</row>
<row>
<entry> Constant View: A proposal for a <code>std::as_const</code> helper function template </entry>
<entry>

View File

@ -112,6 +112,7 @@ bits_headers = \
${bits_srcdir}/hashtable.h \
${bits_srcdir}/hashtable_policy.h \
${bits_srcdir}/indirect_array.h \
${bits_srcdir}/invoke.h \
${bits_srcdir}/ios_base.h \
${bits_srcdir}/istream.tcc \
${bits_srcdir}/list.tcc \

View File

@ -402,6 +402,7 @@ bits_headers = \
${bits_srcdir}/hashtable.h \
${bits_srcdir}/hashtable_policy.h \
${bits_srcdir}/indirect_array.h \
${bits_srcdir}/invoke.h \
${bits_srcdir}/ios_base.h \
${bits_srcdir}/istream.tcc \
${bits_srcdir}/list.tcc \

View File

@ -0,0 +1,104 @@
// Implementation of INVOKE -*- C++ -*-
// 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.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/bits/invoke.h
* This is an internal header file, included by other library headers.
* Do not attempt to use it directly. @headername{functional}
*/
#ifndef _GLIBCXX_INVOKE_H
#define _GLIBCXX_INVOKE_H 1
#pragma GCC system_header
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else
#include <type_traits>
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/**
* @addtogroup utilities
* @{
*/
// Used by __invoke_impl instead of std::forward<_Tp> so that a
// reference_wrapper is converted to an lvalue-reference.
template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
constexpr _Up&&
__invfwd(typename remove_reference<_Tp>::type& __t) noexcept
{ return static_cast<_Up&&>(__t); }
template<typename _Res, typename _Fn, typename... _Args>
constexpr _Res
__invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
{ return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
constexpr _Res
__invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
_Args&&... __args)
{ return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
constexpr _Res
__invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
_Args&&... __args)
{
return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
}
template<typename _Res, typename _MemPtr, typename _Tp>
constexpr _Res
__invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
{ return __invfwd<_Tp>(__t).*__f; }
template<typename _Res, typename _MemPtr, typename _Tp>
constexpr _Res
__invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
{ return (*std::forward<_Tp>(__t)).*__f; }
/// Invoke a callable object.
template<typename _Callable, typename... _Args>
constexpr typename result_of<_Callable&&(_Args&&...)>::type
__invoke(_Callable&& __fn, _Args&&... __args)
noexcept(__is_nothrow_callable<_Callable(_Args&&...)>::value)
{
using __result_of = result_of<_Callable&&(_Args&&...)>;
using __type = typename __result_of::type;
using __tag = typename __result_of::__invoke_type;
return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
std::forward<_Args>(__args)...);
}
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++11
#endif // _GLIBCXX_INVOKE_H

View File

@ -36,7 +36,7 @@
#else
#include <tuple>
#include <functional>
#include <bits/invoke.h>
namespace std _GLIBCXX_VISIBILITY(default)
{
@ -54,20 +54,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template <typename _Fn, typename _Tuple, std::size_t... _Idx>
constexpr decltype(auto)
__apply_impl(_Fn&& f, _Tuple&& t, std::index_sequence<_Idx...>)
__apply_impl(_Fn&& __f, _Tuple&& __t, std::index_sequence<_Idx...>)
{
using _Wrap = _Maybe_wrap_member_pointer<decay_t<_Fn>>;
return _Wrap::__do_wrap(std::forward<_Fn>(f))(
std::get<_Idx>(std::forward<_Tuple>(t))...);
return std::__invoke(std::forward<_Fn>(__f),
std::get<_Idx>(std::forward<_Tuple>(__t))...);
}
template <typename _Fn, typename _Tuple>
constexpr decltype(auto)
apply(_Fn&& f, _Tuple&& t)
apply(_Fn&& __f, _Tuple&& __t)
{
using _Indices =
std::make_index_sequence<std::tuple_size<std::decay_t<_Tuple>>::value>;
return __apply_impl(std::forward<_Fn>(f), std::forward<_Tuple>(t),
std::make_index_sequence<tuple_size_v<std::decay_t<_Tuple>>>;
return __apply_impl(std::forward<_Fn>(__f), std::forward<_Tuple>(__t),
_Indices{});
}

View File

@ -56,6 +56,7 @@
#include <type_traits>
#include <bits/functexcept.h>
#include <bits/functional_hash.h>
#include <bits/invoke.h>
namespace std _GLIBCXX_VISIBILITY(default)
{
@ -184,55 +185,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: _Weak_result_type_impl<typename remove_cv<_Functor>::type>
{ };
// Used by __invoke_impl instead of std::forward<_Tp> so that a
// reference_wrapper is converted to an lvalue-reference.
template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
inline _Up&&
__invfwd(typename remove_reference<_Tp>::type& __t) noexcept
{ return static_cast<_Up&&>(__t); }
template<typename _Res, typename _Fn, typename... _Args>
inline _Res
__invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
{ return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }
template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
inline _Res
__invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
_Args&&... __args)
{ return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }
template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
inline _Res
__invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
_Args&&... __args)
{
return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
}
template<typename _Res, typename _MemPtr, typename _Tp>
inline _Res
__invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
{ return __invfwd<_Tp>(__t).*__f; }
template<typename _Res, typename _MemPtr, typename _Tp>
inline _Res
__invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
{ return (*std::forward<_Tp>(__t)).*__f; }
/// Invoke a callable object.
template<typename _Callable, typename... _Args>
inline typename result_of<_Callable&&(_Args&&...)>::type
__invoke(_Callable&& __fn, _Args&&... __args)
noexcept(__is_nothrow_callable<_Callable(_Args...)>::value)
{
using __result_of = result_of<_Callable&&(_Args&&...)>;
using __type = typename __result_of::type;
using __tag = typename __result_of::__invoke_type;
return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
std::forward<_Args>(__args)...);
}
#if __cplusplus > 201402L
# define __cpp_lib_invoke 201411
@ -240,7 +192,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Callable, typename... _Args>
inline result_of_t<_Callable&&(_Args&&...)>
invoke(_Callable&& __fn, _Args&&... __args)
noexcept(is_nothrow_callable_v<_Callable(_Args...)>)
noexcept(is_nothrow_callable_v<_Callable(_Args&&...)>)
{
return std::__invoke(std::forward<_Callable>(__fn),
std::forward<_Args>(__args)...);

View File

@ -38,6 +38,7 @@
#include <utility>
#include <array>
#include <bits/uses_allocator.h>
#include <bits/invoke.h>
namespace std _GLIBCXX_VISIBILITY(default)
{
@ -1635,6 +1636,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
{ }
#if __cplusplus > 201402L
# define __cpp_lib_apply 201603
template <typename _Fn, typename _Tuple, size_t... _Idx>
constexpr decltype(auto)
__apply_impl(_Fn&& __f, _Tuple&& __t, index_sequence<_Idx...>)
{
return std::__invoke(std::forward<_Fn>(__f),
std::get<_Idx>(std::forward<_Tuple>(__t))...);
}
template <typename _Fn, typename _Tuple>
constexpr decltype(auto)
apply(_Fn&& __f, _Tuple&& __t)
{
using _Indices = make_index_sequence<tuple_size_v<decay_t<_Tuple>>>;
return __apply_impl(std::forward<_Fn>(__f), std::forward<_Tuple>(__t),
_Indices{});
}
#endif // C++17
/// @}
_GLIBCXX_END_NAMESPACE_VERSION

View File

@ -0,0 +1,67 @@
// Copyright (C) 2014-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++17" }
#include <tuple>
#include <testsuite_hooks.h>
#if __cpp_lib_apply < 201603
# error "__cpp_lib_apply < 201603"
#endif
void
test01()
{
auto t = std::make_tuple(1, '2', 3.0);
std::apply( [&](int& i, char& c, double& d) {
VERIFY(&i == &std::get<int>(t));
VERIFY(&c == &std::get<char>(t));
VERIFY(&d == &std::get<double>(t));
}, t);
}
constexpr int func(int i, int j) { return i + j; }
void
test02()
{
constexpr auto t = std::make_tuple(1, 2);
constexpr int i = std::apply(func, t);
VERIFY( i == 3 );
}
struct F
{
int f(int i, int j) const { return i + j; }
};
void
test03()
{
auto t = std::make_tuple(F{}, 1, 2);
int r = std::apply(&F::f, t);
VERIFY( r == 3 );
}
int
main()
{
test01();
test02();
test03();
}

View File

@ -17,7 +17,7 @@
// { dg-options "-fno-show-column" }
// { dg-do compile { target c++14 } }
// { dg-error "in range" "" { target *-*-* } 1279 }
// { dg-error "in range" "" { target *-*-* } 1280 }
#include <tuple>