From patchwork Wed Oct 23 19:28:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1182440 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-511622-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="IBNbDDNs"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="Q9ggcB2N"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46z0ms2vJfz9sNx for ; Thu, 24 Oct 2019 06:29:17 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=lmEh66LaQODAQLdro+hSg5zqRwD6LV2UIk1+SHqVb0mQf3REtH0zW ta9JEBFeiN25PubfFuO4Cl53ltKMGWhGlhKQN1JwmKizpTP5BVRjj1YPt8x0W0f7 h/v3iRGiRBlo+7k4c9eSJoRtm0ZjJLcCpNhDX9vukJAd2yxG4eWNyQ= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=Xnp8YuzGL4pXuSLfFJ0lW8SMtSQ=; b=IBNbDDNsPg223JUM7vw8 bqkwGXc8+VQMEEXjUJRZ0H8WGTud/e/Hwcl4x0FGzVzGiMuK73gtBk93YxoztKYu CQMf0SwGnTxyBaDFZNFb5El4sHXTDx55NUntKq/qGOY+990ZWXvP9PzsU/xCNg9u VlyWUZ0+wSizPs7SLCwkNyE= Received: (qmail 28279 invoked by alias); 23 Oct 2019 19:29:05 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 28215 invoked by uid 89); 23 Oct 2019 19:29:05 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-16.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_PASS autolearn=unavailable version=3.3.1 spammy= X-HELO: us-smtp-delivery-1.mimecast.com Received: from us-smtp-2.mimecast.com (HELO us-smtp-delivery-1.mimecast.com) (205.139.110.61) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 23 Oct 2019 19:29:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571858942; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=HLs40J/6zSqCvQJ3gFT8OUbEHFH9a1nmwzU7cgZ2RGM=; b=Q9ggcB2NvjOwVWksThXYjAUV0iATtrhdhQpmwYSBOtQMjpJIvXjscS1plvZSJAgA4BDkXe kuTKnjxXiDurh3JztiK+UuXFsb77olprBpJkBMcFnfEHZlQiYr/YqsZvoYRVjJtwYc+hT6 qAivwhhIHoktYHE+zCiegiyGkHGiqjI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-36-WXU_s2-ONMSzzHAg3KXRcg-1; Wed, 23 Oct 2019 15:28:58 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BC6365E9; Wed, 23 Oct 2019 19:28:57 +0000 (UTC) Received: from localhost (unknown [10.33.36.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6C9101000232; Wed, 23 Oct 2019 19:28:57 +0000 (UTC) Date: Wed, 23 Oct 2019 20:28:56 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Make std::invoke usable in constant expressions Message-ID: <20191023192856.GA9608@redhat.com> MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.12.1 (2019-06-15) X-Mimecast-Spam-Score: 0 Content-Disposition: inline * 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. commit dae24283df4341aa66f455c8cee6f7935470f7b5 Author: Jonathan Wakely 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 - 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 );