Skip to content

Commit 7f69a39

Browse files
[libc++] Deprecate and remove member types of hash in <variant> (#127758)
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.
1 parent 43e83b9 commit 7f69a39

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

libcxx/include/__variant/monostate.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>=(monostate, monostate) noe
4949

5050
template <>
5151
struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
52-
using argument_type = monostate;
53-
using result_type = size_t;
52+
# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
53+
using argument_type _LIBCPP_DEPRECATED_IN_CXX17 = monostate;
54+
using result_type _LIBCPP_DEPRECATED_IN_CXX17 = size_t;
55+
# endif
5456

55-
inline _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type&) const _NOEXCEPT {
57+
inline _LIBCPP_HIDE_FROM_ABI size_t operator()(const monostate&) const noexcept {
5658
return 66740831; // return a fundamentally attractive random value.
5759
}
5860
};

libcxx/include/variant

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,10 +1585,12 @@ swap(variant<_Types...>& __lhs,
15851585

15861586
template <class... _Types>
15871587
struct _LIBCPP_TEMPLATE_VIS hash< __enable_hash_helper<variant<_Types...>, remove_const_t<_Types>...>> {
1588-
using argument_type = variant<_Types...>;
1589-
using result_type = size_t;
1588+
# if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
1589+
using argument_type _LIBCPP_DEPRECATED_IN_CXX17 = variant<_Types...>;
1590+
using result_type _LIBCPP_DEPRECATED_IN_CXX17 = size_t;
1591+
# endif
15901592

1591-
_LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type& __v) const {
1593+
_LIBCPP_HIDE_FROM_ABI size_t operator()(const variant<_Types...>& __v) const {
15921594
using __variant_detail::__visitation::__variant;
15931595
size_t __res =
15941596
__v.valueless_by_exception()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// REQUIRES: std-at-least-c++17
10+
11+
#include <variant>
12+
13+
#include "test_macros.h"
14+
15+
using A1 [[maybe_unused]] = std::hash<std::variant<int, long>>::argument_type;
16+
using R1 [[maybe_unused]] = std::hash<std::variant<int, long>>::result_type;
17+
#if TEST_STD_VER >= 20
18+
// expected-error@-3 {{no type named 'argument_type' in 'std::hash<std::variant<int, long>>'}}
19+
// expected-error@-3 {{no type named 'result_type' in 'std::hash<std::variant<int, long>>'}}
20+
#else
21+
// expected-warning@-6 {{'argument_type' is deprecated}}
22+
// expected-warning@-6 {{'result_type' is deprecated}}
23+
#endif
24+
25+
using A2 [[maybe_unused]] = std::hash<std::monostate>::argument_type;
26+
using R2 [[maybe_unused]] = std::hash<std::monostate>::result_type;
27+
#if TEST_STD_VER >= 20
28+
// expected-error@-3 {{no type named 'argument_type' in 'std::hash<monostate>'}}
29+
// expected-error@-3 {{no type named 'result_type' in 'std::hash<monostate>'}}
30+
#else
31+
// expected-warning@-6 {{'argument_type' is deprecated}}
32+
// expected-warning@-6 {{'result_type' is deprecated}}
33+
#endif

0 commit comments

Comments
 (0)