From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 553D43857003; Thu, 25 Feb 2021 11:54:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 553D43857003 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-7387] libstdc++: Add std::to_underlying for C++23 X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: a21b99e8cebc990f7b62ed1ebadb582082e922b6 X-Git-Newrev: 7244879b883cb3405bdd98f051d50a2f36394e79 Message-Id: <20210225115408.553D43857003@sourceware.org> Date: Thu, 25 Feb 2021 11:54:08 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 25 Feb 2021 11:54:08 -0000 https://gcc.gnu.org/g:7244879b883cb3405bdd98f051d50a2f36394e79 commit r11-7387-g7244879b883cb3405bdd98f051d50a2f36394e79 Author: Jonathan Wakely Date: Thu Feb 25 11:20:17 2021 +0000 libstdc++: Add std::to_underlying for C++23 Implement P1682R2 as just approved for C++23. libstdc++-v3/ChangeLog: * include/std/utility (to_underlying): Define. * include/std/version (__cpp_lib_to_underlying): Define. * testsuite/20_util/to_underlying/1.cc: New test. * testsuite/20_util/to_underlying/version.cc: New test. Diff: --- libstdc++-v3/include/std/utility | 9 +++++ libstdc++-v3/include/std/version | 3 +- libstdc++-v3/testsuite/20_util/to_underlying/1.cc | 38 ++++++++++++++++++++++ .../testsuite/20_util/to_underlying/version.cc | 27 +++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility index 4c7fca96c5e..fb19d62968f 100644 --- a/libstdc++-v3/include/std/utility +++ b/libstdc++-v3/include/std/utility @@ -461,6 +461,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else return __t <= make_unsigned_t<_Up>(__int_traits<_Up>::__max); } + +#if __cplusplus > 202002L +#define __cpp_lib_to_underlying 202102L + /// Convert an object of enumeration type to its underlying type. + template + constexpr underlying_type_t<_Tp> + to_underlying(_Tp __value) noexcept + { return static_cast>(__value); } +#endif // C++23 #endif // C++20 #endif // C++17 diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index ace87cf42cf..e9eca06a72a 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -250,7 +250,7 @@ #define __cpp_lib_ssize 201902L #define __cpp_lib_starts_ends_with 201711L # if _GLIBCXX_USE_CXX11_ABI -// Only supported with cx11-abi +// Only supported with cxx11-abi # define __cpp_lib_syncbuf 201803L # endif #define __cpp_lib_to_address 201711L @@ -260,6 +260,7 @@ #if __cplusplus > 202002L // c++2b #define __cpp_lib_string_contains 202011L +#define __cpp_lib_to_underlying 202102L #endif // C++2b #endif // C++20 #endif // C++17 diff --git a/libstdc++-v3/testsuite/20_util/to_underlying/1.cc b/libstdc++-v3/testsuite/20_util/to_underlying/1.cc new file mode 100644 index 00000000000..5d2b150230f --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/to_underlying/1.cc @@ -0,0 +1,38 @@ +// 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_to_underlying +# error "Feature-test macro for to_underlying missing in " +#elif __cpp_lib_to_underlying != 202102L +# error "Feature-test macro for to_underlying has wrong value in " +#endif + +void +test01() +{ + enum E : short { e0, e1, e2 }; + E e = e0; + static_assert( std::is_same_v ); + static_assert( noexcept(std::to_underlying(e)) ); + static_assert( std::to_underlying(e1) == 1 ); + static_assert( std::to_underlying((E)3) == 3 ); +} diff --git a/libstdc++-v3/testsuite/20_util/to_underlying/version.cc b/libstdc++-v3/testsuite/20_util/to_underlying/version.cc new file mode 100644 index 00000000000..418bbff3702 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/to_underlying/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_to_underlying +# error "Feature-test macro for to_underlying missing in " +#elif __cpp_lib_to_underlying != 202102L +# error "Feature-test macro for to_underlying has wrong value in " +#endif