[libcxx-commits] [libcxx] [libc++] Deprecate and remove member types of `hash` in `<variant>` (PR #127758)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Feb 18 23:50:56 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: A. Jiang (frederick-vs-ja)
<details>
<summary>Changes</summary>
These member types were deprecated in C++17 by P0174R2 and removed in C++20 by P0619R4, but the changes in `<variant>` seem missing.
Drive-by: Replace one `_NOEXCEPT` with `noexcept` as the `hash` specialization is C++17-and-later only.
---
Full diff: https://github.com/llvm/llvm-project/pull/127758.diff
3 Files Affected:
- (modified) libcxx/include/__variant/monostate.h (+5-3)
- (modified) libcxx/include/variant (+5-3)
- (added) libcxx/test/std/utilities/variant/variant.hash/hash.depr.verify.cpp (+33)
``````````diff
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..a27b627b20ddf
--- /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<std::monostate>'}}
+// expected-error at -3 {{no type named 'result_type' in 'std::hash<std::monostate>'}}
+#else
+// expected-warning at -6 {{'argument_type' is deprecated}}
+// expected-warning at -6 {{'result_type' is deprecated}}
+#endif
``````````
</details>
https://github.com/llvm/llvm-project/pull/127758
More information about the libcxx-commits
mailing list