]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Add std::to_underlying for C++23
authorJonathan Wakely <jwakely@redhat.com>
Thu, 25 Feb 2021 11:20:17 +0000 (11:20 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 25 Feb 2021 11:53:58 +0000 (11:53 +0000)
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.

libstdc++-v3/include/std/utility
libstdc++-v3/include/std/version
libstdc++-v3/testsuite/20_util/to_underlying/1.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/to_underlying/version.cc [new file with mode: 0644]

index 4c7fca96c5e5fb76da93eb18963d083e72e49d08..fb19d62968f2fc6213b1f18e1c2435a82fd617b4 100644 (file)
@@ -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<typename _Tp>
+    constexpr underlying_type_t<_Tp>
+    to_underlying(_Tp __value) noexcept
+    { return static_cast<underlying_type_t<_Tp>>(__value); }
+#endif // C++23
 #endif // C++20
 #endif // C++17
 
index ace87cf42cfbd8c023c176f2f28f59100627a467..e9eca06a72af085e9590be6d105b0b2bb2aee5bc 100644 (file)
 #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
 #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 (file)
index 0000000..5d2b150
--- /dev/null
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <utility>
+
+#ifndef __cpp_lib_to_underlying
+# error "Feature-test macro for to_underlying missing in <utility>"
+#elif __cpp_lib_to_underlying != 202102L
+# error "Feature-test macro for to_underlying has wrong value in <utility>"
+#endif
+
+void
+test01()
+{
+  enum E : short { e0, e1, e2 };
+  E e = e0;
+  static_assert( std::is_same_v<decltype(std::to_underlying(e)), short> );
+  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 (file)
index 0000000..418bbff
--- /dev/null
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <version>
+
+#ifndef __cpp_lib_to_underlying
+# error "Feature-test macro for to_underlying missing in <version>"
+#elif __cpp_lib_to_underlying != 202102L
+# error "Feature-test macro for to_underlying has wrong value in <version>"
+#endif
This page took 0.070416 seconds and 5 git commands to generate.