Add std::apply for C++17
authorJonathan Wakely <jwakely@redhat.com>
Sat, 6 Aug 2016 00:06:02 +0000 (01:06 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Sat, 6 Aug 2016 00:06:02 +0000 (01:06 +0100)
* 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

libstdc++-v3/ChangeLog
libstdc++-v3/doc/html/manual/status.html
libstdc++-v3/doc/xml/manual/status_cxx2017.xml
libstdc++-v3/include/Makefile.am
libstdc++-v3/include/Makefile.in
libstdc++-v3/include/bits/invoke.h [new file with mode: 0644]
libstdc++-v3/include/experimental/tuple
libstdc++-v3/include/std/functional
libstdc++-v3/include/std/tuple
libstdc++-v3/testsuite/20_util/tuple/apply/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/tuple/element_access/get_neg.cc

index e6bf4c95d70f0ed5a3f93e995e9df47f6b6bf264..81fb9e38ba74dd322943a748e7b7157a588ea1cd 100644 (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.
index c690cf3ac910b76744d7b0a8039c129b53bc875e..35b31c1720db97276e7529117b8c454dffe5f047 100644 (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>
index 02aec2571f9e140b1003ae0a836db259177145b4..55e3ff59d97233450a4c9b48bd53ccbbc9204d61 100644 (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>
index e2c4f63d623a322770159f2c3ecaa76e493a5908..ea992f05319a486e35b889d2cd7e4a39ec97929b 100644 (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 \
index 882ff14557dafc81c12a8f0652c40127132f2f7d..b30eeae5f49ae4fefb2a6caa4bf95dce61c95caf 100644 (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 \
diff --git a/libstdc++-v3/include/bits/invoke.h b/libstdc++-v3/include/bits/invoke.h
new file mode 100644 (file)
index 0000000..60405b5
--- /dev/null
@@ -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
index 81e91bda3735148d367f6a3c157332cc0adb0157..bfa1ed18f9e1581e0c58c7c78209882883969531 100644 (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{});
     }
 
index 4ca32c3ef077bd25d4d72d5ad447ef233889867c..1de914e4a2efcae209fbb7f8ca369ab521a7fbf1 100644 (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)...);
index 484cb4822f24a727ab7e00c66905035fb607a50b..b9074cb1bd90b645994581d86b7188f9f376ea61 100644 (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
diff --git a/libstdc++-v3/testsuite/20_util/tuple/apply/1.cc b/libstdc++-v3/testsuite/20_util/tuple/apply/1.cc
new file mode 100644 (file)
index 0000000..c12309c
--- /dev/null
@@ -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();
+}
index 95ff697ae7a7ccf98df1c3cd3e110ae37584a8fd..5bcf5761c360ed01bdfa4f2ae68f02178142cc4b 100644 (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>