[PATCH] Make std::invoke usable in constant expressions

Jonathan Wakely jwakely@redhat.com
Wed Oct 23 19:39:00 GMT 2019


	* include/std/functional (invoke): Add constexpr for C++20.
	* include/std/version (__cpp_lib_constexpr_invoke): Define.
	* testsuite/20_util/function_objects/invoke/constexpr.cc: New test.

This is an easy one, because I already made std::__invoke constexpr,
so all that's needed for C++20 is to add _GLIBCXX20_CONSTEXPR to the
public std::invoke function that calls std::__invoke.

Tested powerpc64le-linux, committed to trunk.


-------------- next part --------------
commit dae24283df4341aa66f455c8cee6f7935470f7b5
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Oct 23 17:40:42 2019 +0100

    Make std::invoke usable in constant expressions
    
            * include/std/functional (invoke): Add constexpr for C++20.
            * include/std/version (__cpp_lib_constexpr_invoke): Define.
            * testsuite/20_util/function_objects/invoke/constexpr.cc: New test.

diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional
index 7ad29a1a335..113a13b4a37 100644
--- a/libstdc++-v3/include/std/functional
+++ b/libstdc++-v3/include/std/functional
@@ -72,19 +72,22 @@ namespace std _GLIBCXX_VISIBILITY(default)
 {
 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
-#if __cplusplus > 201402L
-# define __cpp_lib_invoke 201411
+#if __cplusplus >= 201703L
+# define __cpp_lib_invoke 201411L
+# if __cplusplus > 201703L
+#  define __cpp_lib_constexpr_invoke 201907L
+# endif
 
   /// Invoke a callable object.
   template<typename _Callable, typename... _Args>
-    inline invoke_result_t<_Callable, _Args...>
+    inline _GLIBCXX20_CONSTEXPR invoke_result_t<_Callable, _Args...>
     invoke(_Callable&& __fn, _Args&&... __args)
     noexcept(is_nothrow_invocable_v<_Callable, _Args...>)
     {
       return std::__invoke(std::forward<_Callable>(__fn),
 			   std::forward<_Args>(__args)...);
     }
-#endif
+#endif // C++17
 
   template<typename _MemFunPtr,
 	   bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version
index 21cc28b3450..ccaedd090b0 100644
--- a/libstdc++-v3/include/std/version
+++ b/libstdc++-v3/include/std/version
@@ -158,6 +158,7 @@
 #endif
 #define __cpp_lib_constexpr 201711L
 #define __cpp_lib_constexpr_algorithms 201806L
+#define __cpp_lib_constexpr_invoke 201907L
 #if __cpp_impl_destroying_delete
 # define __cpp_lib_destroying_delete 201806L
 #endif
diff --git a/libstdc++-v3/testsuite/20_util/function_objects/invoke/constexpr.cc b/libstdc++-v3/testsuite/20_util/function_objects/invoke/constexpr.cc
new file mode 100644
index 00000000000..f65caa21936
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/function_objects/invoke/constexpr.cc
@@ -0,0 +1,38 @@
+// Copyright (C) 2019 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 <functional>
+
+#ifndef __cpp_lib_constexpr_invoke
+# error "Feature test macro for constexpr invoke is missing"
+#elif __cpp_lib_constexpr_invoke < 201907L
+# error "Feature test macro for constexpr invoke has wrong value"
+#endif
+
+constexpr int inc(int i) { return i + 1; }
+constexpr auto inc_f = &inc;
+static_assert( std::invoke(inc_f, 2) == 3);
+
+struct Dec
+{
+  constexpr int operator()(int i) const { return i - 1; }
+};
+
+static_assert( std::invoke(Dec{}, 5) == 4 );


More information about the Gcc-patches mailing list