Skip to content

Commit 99cfe6a

Browse files
committed
[libcxx] Put std::monostate in <utility>
1 parent d2616cc commit 99cfe6a

File tree

6 files changed

+211
-1
lines changed

6 files changed

+211
-1
lines changed

libcxx/docs/ReleaseNotes/21.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Implemented Papers
4040

4141
- N4258: Cleaning-up noexcept in the Library (`Github <https://github.com/llvm/llvm-project/issues/99937>`__)
4242
- P1361R2: Integration of chrono with text formatting (`Github <https://github.com/llvm/llvm-project/issues/100014>`__)
43+
- P0472R3: Put std::monostate in <utility> (`Github <https://github.com/llvm/llvm-project/issues/127874`__)
4344

4445
Improvements and New Features
4546
-----------------------------

libcxx/docs/Status/Cxx2cPapers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
"`P3475R2 <https://wg21.link/P3475R2>`__","Defang and deprecate ``memory_order::consume``","2025-02 (Hagenberg)","","",""
103103
"`P2786R13 <https://wg21.link/P2786R13>`__","Trivial Relocatability For C++26","2025-02 (Hagenberg)","","",""
104104
"`P3137R3 <https://wg21.link/P3137R3>`__","``views::to_input``","2025-02 (Hagenberg)","","",""
105-
"`P0472R3 <https://wg21.link/P0472R3>`__","Put ``std::monostate`` in ``<utility>``","2025-02 (Hagenberg)","","",""
105+
"`P0472R3 <https://wg21.link/P0472R3>`__","Put ``std::monostate`` in ``<utility>``","2025-02 (Hagenberg)","|Complete|","21",""
106106
"`P3349R1 <https://wg21.link/P3349R1>`__","Converting contiguous iterators to pointers","2025-02 (Hagenberg)","","",""
107107
"`P3372R3 <https://wg21.link/P3372R3>`__","constexpr containers and adaptors","2025-02 (Hagenberg)","","",""
108108
"`P3378R2 <https://wg21.link/P3378R2>`__","constexpr exception types","2025-02 (Hagenberg)","","",""
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
// -*- C++ -*-
3+
//===----------------------------------------------------------------------===//
4+
//
5+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
// See https://llvm.org/LICENSE.txt for license information.
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
#ifndef _LIBCPP___CXX03___UTILITY_MONOSTATE_H
12+
#define _LIBCPP___CXX03___UTILITY_MONOSTATE_H
13+
14+
#include <__cxx03/__compare/ordering.h>
15+
#include <__cxx03/__config>
16+
#include <__cxx03/__functional/hash.h>
17+
#include <__cxx03/cstddef>
18+
19+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20+
# pragma GCC system_header
21+
#endif
22+
23+
_LIBCPP_BEGIN_NAMESPACE_STD
24+
25+
#if _LIBCPP_STD_VER >= 17
26+
27+
struct _LIBCPP_TEMPLATE_VIS monostate {};
28+
29+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(monostate, monostate) noexcept { return true; }
30+
31+
# if _LIBCPP_STD_VER >= 20
32+
33+
_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(monostate, monostate) noexcept {
34+
return strong_ordering::equal;
35+
}
36+
37+
# else // _LIBCPP_STD_VER >= 20
38+
39+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator!=(monostate, monostate) noexcept { return false; }
40+
41+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<(monostate, monostate) noexcept { return false; }
42+
43+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>(monostate, monostate) noexcept { return false; }
44+
45+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<=(monostate, monostate) noexcept { return true; }
46+
47+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>=(monostate, monostate) noexcept { return true; }
48+
49+
# endif // _LIBCPP_STD_VER >= 20
50+
51+
template <>
52+
struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
53+
using argument_type = monostate;
54+
using result_type = size_t;
55+
56+
inline _LIBCPP_HIDE_FROM_ABI result_type operator()(const argument_type&) const _NOEXCEPT {
57+
return 66740831; // return a fundamentally attractive random value.
58+
}
59+
};
60+
61+
#endif // _LIBCPP_STD_VER >= 17
62+
63+
_LIBCPP_END_NAMESPACE_STD
64+
65+
#endif // _LIBCPP___CXX03___UTILITY_MONOSTATE_H
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// -*- C++ -*-
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef _LIBCPP___UTILITY_MONOSTATE_H
11+
#define _LIBCPP___UTILITY_MONOSTATE_H
12+
13+
#include <__compare/ordering.h>
14+
#include <__configUTILITY>
15+
#include <__cstddef/size_t.h>
16+
#include <__functional/hash.h>
17+
18+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19+
# pragma GCC system_header
20+
#endif
21+
22+
_LIBCPP_BEGIN_NAMESPACE_STD
23+
24+
#if _LIBCPP_STD_VER >= 17
25+
26+
struct _LIBCPP_TEMPLATE_VIS monostate {};
27+
28+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(monostate, monostate) noexcept { return true; }
29+
30+
# if _LIBCPP_STD_VER >= 20
31+
32+
_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(monostate, monostate) noexcept {
33+
return strong_ordering::equal;
34+
}
35+
36+
# else // _LIBCPP_STD_VER >= 20
37+
38+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator!=(monostate, monostate) noexcept { return false; }
39+
40+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<(monostate, monostate) noexcept { return false; }
41+
42+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>(monostate, monostate) noexcept { return false; }
43+
44+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator<=(monostate, monostate) noexcept { return true; }
45+
46+
_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator>=(monostate, monostate) noexcept { return true; }
47+
48+
# endif // _LIBCPP_STD_VER >= 20
49+
50+
template <>
51+
struct _LIBCPP_TEMPLATE_VIS hash<monostate> {
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
56+
57+
inline _LIBCPP_HIDE_FROM_ABI size_t operator()(const monostate&) const noexcept {
58+
return 66740831; // return a fundamentally attractive random value.
59+
}
60+
};
61+
62+
#endif // _LIBCPP_STD_VER >= 17
63+
64+
_LIBCPP_END_NAMESPACE_STD
65+
66+
#endif // _LIBCPP___UTILITY_MONOSTATE_H
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
10+
11+
// <utility>
12+
13+
// constexpr bool operator<(monostate, monostate) noexcept { return false; }
14+
// constexpr bool operator>(monostate, monostate) noexcept { return false; }
15+
// constexpr bool operator<=(monostate, monostate) noexcept { return true; }
16+
// constexpr bool operator>=(monostate, monostate) noexcept { return true; }
17+
// constexpr bool operator==(monostate, monostate) noexcept { return true; }
18+
// constexpr bool operator!=(monostate, monostate) noexcept { return false; }
19+
// constexpr strong_ordering operator<=>(monostate, monostate) noexcept { return strong_ordering::equal; } // since C++20
20+
21+
#include <cassert>
22+
#include <utility>
23+
24+
#include "test_comparisons.h"
25+
#include "test_macros.h"
26+
27+
constexpr bool test() {
28+
using M = std::monostate;
29+
constexpr M m1{};
30+
constexpr M m2{};
31+
assert(testComparisons(m1, m2, /*isEqual*/ true, /*isLess*/ false));
32+
AssertComparisonsAreNoexcept<M>();
33+
34+
#if TEST_STD_VER > 17
35+
assert(testOrder(m1, m2, std::strong_ordering::equal));
36+
AssertOrderAreNoexcept<M>();
37+
#endif // TEST_STD_VER > 17
38+
39+
return true;
40+
}
41+
42+
int main(int, char**) {
43+
test();
44+
static_assert(test());
45+
46+
return 0;
47+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
//===----------------------------------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
11+
12+
// <utility>
13+
14+
// struct monostate {};
15+
16+
#include <type_traits>
17+
#include <utility>
18+
19+
#include "test_macros.h"
20+
21+
int main(int, char**) {
22+
using M = std::monostate;
23+
static_assert(std::is_trivially_default_constructible<M>::value, "");
24+
static_assert(std::is_trivially_copy_constructible<M>::value, "");
25+
static_assert(std::is_trivially_copy_assignable<M>::value, "");
26+
static_assert(std::is_trivially_destructible<M>::value, "");
27+
constexpr M m{};
28+
((void)m);
29+
30+
return 0;
31+
}

0 commit comments

Comments
 (0)