diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2019-10-23 18:42:16 +0100 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2019-10-23 18:42:16 +0100 |
commit | 6d188e4fddff68f4c50f8178104137f71d4c621e (patch) | |
tree | ddfa9f6b24e40fba176a177fca21c6375c01d465 | |
parent | 85f241149328ce8e677cbdfa7fa8592aca155066 (diff) | |
download | gcc-6d188e4fddff68f4c50f8178104137f71d4c621e.zip gcc-6d188e4fddff68f4c50f8178104137f71d4c621e.tar.gz gcc-6d188e4fddff68f4c50f8178104137f71d4c621e.tar.bz2 |
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.
From-SVN: r277343
-rw-r--r-- | libstdc++-v3/ChangeLog | 4 | ||||
-rw-r--r-- | libstdc++-v3/include/std/functional | 11 | ||||
-rw-r--r-- | libstdc++-v3/include/std/version | 1 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/function_objects/invoke/constexpr.cc | 38 |
4 files changed, 50 insertions, 4 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 1e98ea3..74fd23b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,9 @@ 2019-10-23 Jonathan Wakely <jwakely@redhat.com> + * 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. + PR c++/91369 Implement P0784R7 changes to allocation and construction * include/bits/alloc_traits.h: Include <bits/stl_construct.h>. (allocator_traits::_S_allocate, allocator_traits::_S_construct) diff --git a/libstdc++-v3/include/std/functional b/libstdc++-v3/include/std/functional index 7ad29a1..113a13b 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 21cc28b..ccaedd0 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 0000000..f65caa21 --- /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 ); |