From patchwork Fri Mar 19 20:11:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1456004 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=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=CWCW6C1m; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F2FRF3Vjlz9sSC for ; Sat, 20 Mar 2021 07:11:51 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D9B0A386F438; Fri, 19 Mar 2021 20:11:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D9B0A386F438 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1616184707; bh=CTBttEcHsMlSlEN2e/hi2rQNWcEjU4dCU7PgdgMvhCk=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=CWCW6C1m8tJluScT2qkF6z+6tBNd1V+xv4sMDfteEmE/MJR9GZ6RR4ozN6EE9tjfl bOkV1//bwMOV76Bkm/nyPq71/7GrKRa3gXbWY+MzrXxMpY7vWZbvnEb0Rj7RR9DrGd eALMu1TVBERNR3vgi0mKYkQRepTJhYOZ1VixJdsc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 5CC58385041F for ; Fri, 19 Mar 2021 20:11:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5CC58385041F 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-284-RpnhdTHJN_-qH6cuVeFllw-1; Fri, 19 Mar 2021 16:11:39 -0400 X-MC-Unique: RpnhdTHJN_-qH6cuVeFllw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E4801107ACCA; Fri, 19 Mar 2021 20:11:38 +0000 (UTC) Received: from localhost (unknown [10.33.36.164]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88BDD1F40A; Fri, 19 Mar 2021 20:11:38 +0000 (UTC) Date: Fri, 19 Mar 2021 20:11:37 +0000 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add std::is_scoped_enum for C++23 Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Implement this C++23 feature, as proposed by P1048R1. This implementation assumes that a C++23 compiler supports concepts already. I don't see any point in using preprocessor hacks to detect compilers which define __cplusplus to a post-C++20 value but don't support concepts yet. libstdc++-v3/ChangeLog: * include/std/type_traits (is_scoped_enum): Define. * include/std/version (__cpp_lib_is_scoped_enum): Define. * testsuite/20_util/is_scoped_enum/value.cc: New test. * testsuite/20_util/is_scoped_enum/version.cc: New test. Tested powerpc64le-linux. Committed to trunk. commit b8ecdc772703729b75fba8b4bb94acfcb6f7cfae Author: Jonathan Wakely Date: Fri Mar 19 19:42:18 2021 libstdc++: Add std::is_scoped_enum for C++23 Implement this C++23 feature, as proposed by P1048R1. This implementation assumes that a C++23 compiler supports concepts already. I don't see any point in using preprocessor hacks to detect compilers which define __cplusplus to a post-C++20 value but don't support concepts yet. libstdc++-v3/ChangeLog: * include/std/type_traits (is_scoped_enum): Define. * include/std/version (__cpp_lib_is_scoped_enum): Define. * testsuite/20_util/is_scoped_enum/value.cc: New test. * testsuite/20_util/is_scoped_enum/version.cc: New test. diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index ad2672d36a6..eeab1ca65fc 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -3279,6 +3279,23 @@ template inline constexpr bool is_unbounded_array_v = is_unbounded_array<_Tp>::value; +#if __cplusplus > 202002L +#define __cpp_lib_is_scoped_enum 202011L + + template + struct is_scoped_enum + : false_type + { }; + + template requires __is_enum(_Tp) + struct is_scoped_enum<_Tp> + : __not_>::type + { }; + + template + inline constexpr bool is_scoped_enum_v = is_scoped_enum<_Tp>::value; +#endif // C++23 + #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED #define __cpp_lib_is_constant_evaluated 201811L diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index e9eca06a72a..cb25148fca5 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -259,6 +259,7 @@ #if __cplusplus > 202002L // c++2b +#define __cpp_lib_is_scoped_enum 202011L #define __cpp_lib_string_contains 202011L #define __cpp_lib_to_underlying 202102L #endif // C++2b diff --git a/libstdc++-v3/testsuite/20_util/is_scoped_enum/value.cc b/libstdc++-v3/testsuite/20_util/is_scoped_enum/value.cc new file mode 100644 index 00000000000..bab7263ae4a --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/is_scoped_enum/value.cc @@ -0,0 +1,62 @@ +// Copyright (C) 2021 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++23" } +// { dg-do compile { target c++23 } } + +#include + +#ifndef __cpp_lib_is_scoped_enum +# error "Feature test macro for is_scoped_enum is missing in " +#elif __cpp_lib_is_scoped_enum < 202011L +# error "Feature test macro for is_scoped_enum has wrong value in " +#endif + +#include + +template + concept Is_scoped_enum + = __gnu_test::test_category(true); + +void +test01() +{ + enum class E { e1, e2 }; + static_assert( Is_scoped_enum ); + enum class Ec : char { e1, e2 }; + static_assert( Is_scoped_enum ); + + // negative tests + enum U { u1, u2 }; + static_assert( ! Is_scoped_enum ); + enum F : int { f1, f2 }; + static_assert( ! Is_scoped_enum ); + struct S { }; + static_assert( ! Is_scoped_enum ); + + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); + static_assert( ! Is_scoped_enum ); +} diff --git a/libstdc++-v3/testsuite/20_util/is_scoped_enum/version.cc b/libstdc++-v3/testsuite/20_util/is_scoped_enum/version.cc new file mode 100644 index 00000000000..ed78fce5fbe --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/is_scoped_enum/version.cc @@ -0,0 +1,27 @@ +// Copyright (C) 2021 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++23" } +// { dg-do compile { target c++23 } } + +#include + +#ifndef __cpp_lib_is_scoped_enum +# error "Feature test macro for is_scoped_enum is missing in " +#elif __cpp_lib_is_scoped_enum < 202011L +# error "Feature test macro for is_scoped_enum has wrong value in " +#endif