[libc++] [C++2b] [P1682] Add to_underlying.

* https://wg21.link/P1682

Reviewed By: ldionne, Mordante, #libc

Differential Revision: https://reviews.llvm.org/D97365

GitOrigin-RevId: 43e421417378bab378eccdf88a66a0a46304fa13
diff --git a/include/utility b/include/utility
index 6f27af7..ca3d3fe 100644
--- a/include/utility
+++ b/include/utility
@@ -191,6 +191,10 @@
 template <size_t I>
   inline constexpr in_place_index_t<I> in_place_index{};
 
+// [utility.underlying], to_underlying
+template <class T>
+    constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
+
 }  // std
 
 */
@@ -1622,8 +1626,21 @@
 using __enable_hash_helper _LIBCPP_NODEBUG_TYPE = _Type;
 #endif
 
+template <class _Tp>
+_LIBCPP_INLINE_VISIBILITY constexpr typename underlying_type<_Tp>::type
+__to_underlying(_Tp __val) noexcept {
+  return static_cast<typename underlying_type<_Tp>::type>(__val);
+}
 #endif // !_LIBCPP_CXX03_LANG
 
+#if _LIBCPP_STD_VER > 20
+template <class _Tp>
+_LIBCPP_INLINE_VISIBILITY constexpr underlying_type_t<_Tp>
+to_underlying(_Tp __val) noexcept {
+  return _VSTD::__to_underlying(__val);
+}
+#endif
+
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_UTILITY
diff --git a/include/version b/include/version
index 49648a0..becbfa5 100644
--- a/include/version
+++ b/include/version
@@ -150,6 +150,7 @@
 __cpp_lib_to_address                                    201711L <memory>
 __cpp_lib_to_array                                      201907L <array>
 __cpp_lib_to_chars                                      201611L <utility>
+__cpp_lib_to_underlying                                 202102L <utility>
 __cpp_lib_transformation_trait_aliases                  201304L <type_traits>
 __cpp_lib_transparent_operators                         201510L <functional> <memory>
                                                         201210L // C++14
@@ -365,6 +366,7 @@
 // # define __cpp_lib_stacktrace                           202011L
 // # define __cpp_lib_stdatomic_h                          202011L
 # define __cpp_lib_string_contains                      202011L
+# define __cpp_lib_to_underlying                        202102L
 #endif
 
 // clang-format on