From: Jonathan Wakely Date: Wed, 23 Oct 2019 17:42:16 +0000 (+0100) Subject: Make std::invoke usable in constant expressions X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6d188e4fddff68f4c50f8178104137f71d4c621e;p=gcc.git 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 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 1e98ea3219e..74fd23bacbf 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,9 @@ 2019-10-23 Jonathan Wakely + * 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 . (allocator_traits::_S_allocate, allocator_traits::_S_construct) 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 - 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::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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +#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 );