[libcxx-commits] [libcxx] [libc++] Deprecate and remove member types of `hash` in `<variant>` (PR #127758)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 19 00:24:24 PST 2025


https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/127758

>From 0cac756eced23556c99bd0d9bde19c760731a55f Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Wed, 19 Feb 2025 16:24:02 +0800
Subject: [PATCH] [libc++] Deprecate and remove member types of `hash` in
 `<variant>`

These member types were deprecated in C++17 by P0174R2 and removed in
C++20 by P0619R4, but the changes in `<variant>` seem missing.
---
 libcxx/include/__variant/monostate.h          |  8 +++--
 libcxx/include/variant                        |  8 +++--
 .../variant/variant.hash/hash.depr.verify.cpp | 33 +++++++++++++++++++
 3 files changed, 43 insertions(+), 6 deletions(-)
 create mode 100644 libcxx/test/std/utilities/variant/variant.hash/hash.depr.verify.cpp

diff --git a/libcxx/include/__variant/monostate.h b/libcxx/include/__variant/monostate.h
index c5d2dacaf4205..b29bbdf5cdbe4 100644
--- a/libcxx/include/__variant/monostate.h
+++ b/libcxx/include/__variant/monostate.h
@@ -49,10 +49,12 @@ _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>=(monostate, monostate) noe
 
 template <>
 struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
-  using argument_type = monostate;
-  using result_type   = size_t;
+#  if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
+  using argument_type _LIBCPP_DEPRECATED_IN_CXX17 = monostate;
+  using result_type _LIBCPP_DEPRECATED_IN_CXX17   = size_t;
+#  endif
 
-  inline _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type&) const _NOEXCEPT {
+  inline _LIBCPP_HIDE_FROM_ABI size_t operator()(const monostate&) const noexcept {
     return 66740831; // return a fundamentally attractive random value.
   }
 };
diff --git a/libcxx/include/variant b/libcxx/include/variant
index 3786d9524020b..9998d4a457715 100644
--- a/libcxx/include/variant
+++ b/libcxx/include/variant
@@ -1585,10 +1585,12 @@ swap(variant<_Types...>& __lhs,
 
 template <class... _Types>
 struct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
-  using argument_type = variant<_Types...>;
-  using result_type   = size_t;
+#    if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
+  using argument_type _LIBCPP_DEPRECATED_IN_CXX17 = variant<_Types...>;
+  using result_type _LIBCPP_DEPRECATED_IN_CXX17   = size_t;
+#    endif
 
-  _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type& __v) const {
+  _LIBCPP_HIDE_FROM_ABI size_t operator()(const variant<_Types...>& __v) const {
     using __variant_detail::__visitation::__variant;
     size_t __res =
         __v.valueless_by_exception()
diff --git a/libcxx/test/std/utilities/variant/variant.hash/hash.depr.verify.cpp b/libcxx/test/std/utilities/variant/variant.hash/hash.depr.verify.cpp
new file mode 100644
index 0000000000000..9b22cbda9f345
--- /dev/null
+++ b/libcxx/test/std/utilities/variant/variant.hash/hash.depr.verify.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++17
+
+#include <variant>
+
+#include "test_macros.h"
+
+using A1 [[maybe_unused]] = std::hash<std::variant<int, long>>::argument_type;
+using R1 [[maybe_unused]] = std::hash<std::variant<int, long>>::result_type;
+#if TEST_STD_VER >= 20
+// expected-error at -3 {{no type named 'argument_type' in 'std::hash<std::variant<int, long>>'}}
+// expected-error at -3 {{no type named 'result_type' in 'std::hash<std::variant<int, long>>'}}
+#else
+// expected-warning at -6 {{'argument_type' is deprecated}}
+// expected-warning at -6 {{'result_type' is deprecated}}
+#endif
+
+using A2 [[maybe_unused]] = std::hash<std::monostate>::argument_type;
+using R2 [[maybe_unused]] = std::hash<std::monostate>::result_type;
+#if TEST_STD_VER >= 20
+// expected-error at -3 {{no type named 'argument_type' in 'std::hash<monostate>'}}
+// expected-error at -3 {{no type named 'result_type' in 'std::hash<monostate>'}}
+#else
+// expected-warning at -6 {{'argument_type' is deprecated}}
+// expected-warning at -6 {{'result_type' is deprecated}}
+#endif



More information about the libcxx-commits mailing list