GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Variant.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_VARIANT_HPP
5#define GREM_CORE_DATA_VARIANT_HPP
6
8
10
11#include <compare> // std::strong_ordering, std::common_comparison_category_t, std::compare_three_way_result_t
12#include <cstddef> // std::size_t
13#include <cstdint> // std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t
14#include <exception> // std::exception
15#include <functional> // std::hash
16#include <initializer_list> // std::initializer_list
17#include <memory> // std::construct_at, std::destroy_at
18#include <new> // std::launder
19#include <type_traits> // std::is_..._v, std::..._type, std::integral_constant, std::remove_..._t, std::common_reference_t, std::conditional_t, std::is_constant_evaluated
20#include <utility> // std::move, std::forward, std::swap, std::in_place_..., std::...index_sequence, std::as_const
21
22namespace grem {
23
32template <typename... Ts>
33class Variant;
34
35namespace detail {
36
37template <typename T>
38concept bool_convertible = std::is_convertible_v<T, bool>;
39
40template <class T, class U>
41[[nodiscard]] constexpr auto&& forwardLike(U&& x) noexcept { // NOLINT(cppcoreguidelines-missing-std-forward)
42 constexpr bool IS_CONST = std::is_const_v<std::remove_reference_t<T>>;
43 if constexpr (std::is_lvalue_reference_v<T&&>) {
44 if constexpr (IS_CONST) {
45 return std::as_const(x);
46 } else {
47 return static_cast<U&>(x);
48 }
49 } else {
50 if constexpr (IS_CONST) {
51 return std::move(std::as_const(x));
52 } else {
53 return std::move(x);
54 }
55 }
56}
57
58template <auto...>
59struct ConstantList {};
60
61template <std::size_t N, typename Union, auto... MemberPointers>
62struct n_member_pointers {
63 using type = ConstantList<MemberPointers..., &Union::head>;
64};
65
66template <std::size_t N, typename Union, auto... MemberPointers>
67requires(N > 0) struct n_member_pointers<N, Union, MemberPointers...> : n_member_pointers<N - 1, typename Union::Tail, MemberPointers..., &Union::tail> {};
68
69template <std::size_t N, typename Union>
70using n_member_pointers_t = typename n_member_pointers<N, Union>::type;
71
72template <auto... MemberPointers, typename U>
73[[nodiscard]] constexpr auto& getUnionMemberImplementation(ConstantList<MemberPointers...>, U&& u) noexcept {
74 return (std::forward<U>(u).*....*MemberPointers);
75}
76
77template <std::size_t Index, typename U>
78[[nodiscard]] constexpr auto& getUnionMember(U& u) noexcept {
79 return getUnionMemberImplementation(n_member_pointers_t<Index, U>{}, u);
80}
81
82template <typename... Ts>
83union UnionStorage {
84 constexpr UnionStorage() = default;
85
86 template <std::size_t Index, typename... Args>
87 UnionStorage(std::in_place_index_t<Index>, Args&&...) = delete; // NOLINT(cppcoreguidelines-missing-std-forward)
88};
89
90template <typename First, typename... Rest>
91union UnionStorage<First, Rest...> {
92 static constexpr std::size_t size = 1 + sizeof...(Rest);
93
94 using Head = First;
95 using Tail = UnionStorage<Rest...>;
96
97 constexpr UnionStorage()
98 : tail() {}
99
100 template <typename... Args>
101 constexpr UnionStorage(std::in_place_index_t<0>, Args&&... args)
102 : head(std::forward<Args>(args)...) {}
103
104 template <std::size_t Index, typename... Args>
105 constexpr UnionStorage(std::in_place_index_t<Index>, Args&&... args)
106 : tail(std::in_place_index<Index - 1>, std::forward<Args>(args)...) {}
107
108 constexpr UnionStorage(const UnionStorage& other, std::size_t otherIndex) {
109 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
110 (void)(((otherIndex == Indices) ? (construct<Indices>(getUnionMember<Indices>(other)), true) : false) || ...);
111 }(std::make_index_sequence<size>{});
112 }
113
114 constexpr UnionStorage(UnionStorage&& other, std::size_t otherIndex) { // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
115 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
116 (void)(((otherIndex == Indices) ? (construct<Indices>(std::move(getUnionMember<Indices>(other))), true) : false) || ...);
117 }(std::make_index_sequence<size>{});
118 }
119
120 ~UnionStorage() = default;
121 constexpr ~UnionStorage() requires(!std::is_trivially_destructible_v<Head> || !std::is_trivially_destructible_v<Tail>) {}
122
123 constexpr UnionStorage(const UnionStorage&) = default;
124 constexpr UnionStorage(UnionStorage&&) noexcept(
125 std::is_nothrow_move_constructible_v<Head> && // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
126 std::is_nothrow_move_constructible_v<Tail>) = default;
127 constexpr UnionStorage& operator=(const UnionStorage&) = default;
128 constexpr UnionStorage& operator=(UnionStorage&&) noexcept(
129 std::is_nothrow_move_assignable_v<Head> && // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
130 std::is_nothrow_move_assignable_v<Tail>) = default;
131
132 template <std::size_t Index, typename... Args>
133 constexpr void construct(Args&&... args) {
134 std::construct_at(&getUnionMember<Index>(*this), std::forward<Args>(args)...);
135 }
136
137 constexpr void destroy(std::size_t index) noexcept {
138 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
139 (void)(((index == Indices) ? (std::destroy_at(&getUnionMember<Indices>(*this)), true) : false) || ...);
140 }(std::make_index_sequence<size>{});
141 }
142
143 Head head;
144 Tail tail;
145};
146
147template <typename T, std::size_t Index, typename... Ts>
148struct VariantIndexImpl;
149
150template <typename T, std::size_t Index, typename First, typename... Rest>
151struct VariantIndexImpl<T, Index, First, Rest...> : VariantIndexImpl<T, Index + 1, Rest...> {};
152
153template <typename T, std::size_t Index, typename... Rest>
154struct VariantIndexImpl<T, Index, T, Rest...> : std::integral_constant<std::size_t, Index> {};
155
156} // namespace detail
157
159template <typename T, typename V>
160struct variant_has_alternative;
161
162template <typename T, typename First, typename... Rest>
163struct variant_has_alternative<T, Variant<First, Rest...>> : variant_has_alternative<T, Variant<Rest...>> {};
164
165template <typename T>
166struct variant_has_alternative<T, Variant<>> : std::false_type {};
167
168template <typename T, typename... Rest>
169struct variant_has_alternative<T, Variant<T, Rest...>> : std::true_type {};
171
178template <typename T, typename V>
179inline constexpr bool variant_has_alternative_v = variant_has_alternative<T, V>::value;
180
182template <typename T, typename V>
183struct variant_index;
184
185template <typename T, typename... Ts>
186struct variant_index<T, Variant<Ts...>> : detail::VariantIndexImpl<T, 0, Ts...> {};
188
195template <typename T, typename V>
196inline constexpr std::size_t variant_index_v = variant_index<T, V>::value;
197
199template <std::size_t Index, typename V>
200struct variant_alternative;
201
202template <std::size_t Index, typename First, typename... Rest>
203struct variant_alternative<Index, Variant<First, Rest...>> : variant_alternative<Index - 1, Variant<Rest...>> {};
204
205template <typename T, typename... Rest>
206struct variant_alternative<0, Variant<T, Rest...>> {
207 using type = T;
208};
210
217template <std::size_t Index, typename V>
218using variant_alternative_t = typename variant_alternative<Index, V>::type;
219
221template <typename V>
222struct variant_size;
223
224template <typename... Ts>
225struct variant_size<Variant<Ts...>> : std::integral_constant<std::size_t, sizeof...(Ts)> {};
227
233template <typename V>
234inline constexpr std::size_t variant_size_v = variant_size<V>::value;
235
242struct Monostate {};
243
252[[nodiscard]] constexpr bool operator==(Monostate, Monostate) noexcept {
253 return true;
254}
255
264[[nodiscard]] constexpr std::strong_ordering operator<=>(Monostate, Monostate) noexcept {
265 return std::strong_ordering::equal;
266}
267
268} // namespace grem
269
270template <>
271struct std::hash<grem::Monostate> {
272 [[nodiscard]] std::size_t operator()(const grem::Monostate&) const {
273 return 0;
274 }
275};
276
277namespace grem {
278
284struct BadVariantAccess : std::exception {
285 BadVariantAccess() noexcept = default;
286
287 [[nodiscard]] const char* what() const noexcept override {
288 return "Bad variant access.";
289 }
290};
291
292template <typename... Ts>
293class Variant {
294private:
295 static constexpr bool HAS_DEFAULT_CONSTRUCTOR = std::is_default_constructible_v<variant_alternative_t<0, Variant>>;
296 static constexpr bool HAS_COPY_CONSTRUCTOR = (std::is_copy_constructible_v<Ts> && ...);
297 static constexpr bool HAS_MOVE_CONSTRUCTOR = (std::is_move_constructible_v<Ts> && ...);
298 static constexpr bool HAS_COPY_ASSIGNMENT = ((std::is_copy_constructible_v<Ts> && std::is_copy_assignable_v<Ts>) && ...);
299 static constexpr bool HAS_MOVE_ASSIGNMENT = ((std::is_move_constructible_v<Ts> && std::is_move_assignable_v<Ts>) && ...);
300 static constexpr bool HAS_TRIVIAL_COPY_CONSTRUCTOR = (std::is_trivially_copy_constructible_v<Ts> && ...);
301 static constexpr bool HAS_TRIVIAL_MOVE_CONSTRUCTOR = (std::is_trivially_move_constructible_v<Ts> && ...);
302 static constexpr bool HAS_TRIVIAL_COPY_ASSIGNMENT =
303 ((std::is_trivially_copy_constructible_v<Ts> && std::is_trivially_copy_assignable_v<Ts> && std::is_trivially_destructible_v<Ts>) && ...);
304 static constexpr bool HAS_TRIVIAL_MOVE_ASSIGNMENT =
305 ((std::is_trivially_move_constructible_v<Ts> && std::is_trivially_move_assignable_v<Ts> && std::is_trivially_destructible_v<Ts>) && ...);
306 static constexpr bool HAS_TRIVIAL_DESTRUCTOR = (std::is_trivially_destructible_v<Ts> && ...);
307
308 template <typename T>
309 struct SelectAlternativeTypeOverload {
310 template <typename U>
311 T operator()(T, U&& u) requires(requires { T(std::forward<U>(u)); });
312 };
313
314 struct SelectAlternativeTypeOverloadSet : SelectAlternativeTypeOverload<Ts>... {
315 using SelectAlternativeTypeOverload<Ts>::operator()...;
316 };
317
318 template <typename U>
319 static constexpr auto F(U&& u) -> decltype(SelectAlternativeTypeOverloadSet{}(std::forward<U>(u), std::forward<U>(u)));
320
321public:
322 using GREM_private_DerivedFromVariantTag = void;
323
324 // clang-format off
333 std::conditional_t<sizeof...(Ts) < 255ull, std::uint8_t,
334 std::conditional_t<sizeof...(Ts) < 65535ull, std::uint16_t,
335 std::conditional_t<sizeof...(Ts) < 4294967295ull, std::uint32_t,
336 std::uint64_t>>>;
337 // clang-format on
338
342 static constexpr index_type npos = sizeof...(Ts);
343
351 constexpr Variant() noexcept(std::is_nothrow_default_constructible_v<variant_alternative_t<0, Variant>>) requires(HAS_DEFAULT_CONSTRUCTOR)
352 : storage(std::in_place_index<0>)
353 , activeTypeIndex(0) {}
354
367 template <typename U>
368 constexpr Variant(U&& value) noexcept(std::is_nothrow_constructible_v<decltype(F(std::forward<U>(value))), U>)
369 requires(!std::is_same_v<std::remove_cvref_t<U>, Variant> && variant_has_alternative_v<decltype(F(std::forward<U>(value))), Variant> &&
370 std::is_constructible_v<decltype(F(std::forward<U>(value))), U>)
371 : Variant(std::in_place_index<variant_index_v<decltype(F(std::forward<U>(value))), Variant>>, std::forward<U>(value)) {}
372
384 template <typename T, typename... Args>
385 constexpr explicit Variant(std::in_place_type_t<T> type, Args&&... args) requires(variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, Args...>)
386 : Variant(std::in_place_index<variant_index_v<T, Variant>>, std::forward<Args>(args)...) {
387 (void)type;
388 }
389
405 template <typename T, typename U, typename... Args>
406 constexpr explicit Variant(std::in_place_type_t<T> type, std::initializer_list<U> ilist, Args&&... args)
407 requires(variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, std::initializer_list<U>&, Args...>)
408 : Variant(std::in_place_index<variant_index_v<T, Variant>>, ilist, std::forward<Args>(args)...) {
409 (void)type;
410 }
411
423 template <std::size_t Index, typename... Args>
424 constexpr explicit Variant(std::in_place_index_t<Index> index, Args&&... args)
425 requires(Index < sizeof...(Ts) && std::is_constructible_v<variant_alternative_t<Index, Variant>, Args...>)
426 : storage(index, std::forward<Args>(args)...)
427 , activeTypeIndex(Index) {}
428
444 template <std::size_t Index, typename U, typename... Args>
445 constexpr explicit Variant(std::in_place_index_t<Index> index, std::initializer_list<U> ilist, Args&&... args)
446 requires(Index < sizeof...(Ts) && std::is_constructible_v<variant_alternative_t<Index, Variant>, std::initializer_list<U>&, Args...>)
447 : storage(index, ilist, std::forward<Args>(args)...)
448 , activeTypeIndex(Index) {}
449
451 constexpr ~Variant() {
452 destroy();
453 }
454
456 constexpr ~Variant() requires(HAS_TRIVIAL_DESTRUCTOR) = default;
457
459 constexpr Variant(const Variant& other) requires(!HAS_COPY_CONSTRUCTOR) = delete;
460
462 constexpr Variant(const Variant& other) requires(HAS_COPY_CONSTRUCTOR && HAS_TRIVIAL_COPY_CONSTRUCTOR) = default;
463
465 constexpr Variant(const Variant& other) requires(HAS_COPY_CONSTRUCTOR && !HAS_TRIVIAL_COPY_CONSTRUCTOR)
466 : storage(other.storage, other.activeTypeIndex)
467 , activeTypeIndex(other.activeTypeIndex) {}
468
470 constexpr Variant(Variant&& other) noexcept requires(HAS_MOVE_CONSTRUCTOR && HAS_TRIVIAL_MOVE_CONSTRUCTOR) = default;
471
473 constexpr Variant(Variant&& other) noexcept(
474 (std::is_nothrow_move_constructible_v<Ts> && ...)) // NOLINT(performance-noexcept-move-constructor, cppcoreguidelines-noexcept-move-operations)
475 requires(HAS_MOVE_CONSTRUCTOR && !HAS_TRIVIAL_MOVE_CONSTRUCTOR)
476 : storage(std::move(other.storage), other.activeTypeIndex)
477 , activeTypeIndex(other.activeTypeIndex) {}
478
480 constexpr Variant& operator=(const Variant& other) requires(!HAS_COPY_ASSIGNMENT) = delete;
481
483 constexpr Variant& operator=(const Variant& other) requires(HAS_COPY_ASSIGNMENT && HAS_TRIVIAL_COPY_ASSIGNMENT) = default;
484
491 constexpr Variant& operator=(const Variant& other) requires(HAS_COPY_ASSIGNMENT && !HAS_TRIVIAL_COPY_ASSIGNMENT) {
492 if (this == &other) {
493 return *this;
494 }
495 const auto copyAssign = [&]<size_t OtherIndex, typename Other>(std::in_place_index_t<OtherIndex>, const Other& other) -> void {
496 if (activeTypeIndex == OtherIndex) {
497 as<OtherIndex>() = other;
498 } else if constexpr (std::is_nothrow_copy_constructible_v<Other> || !std::is_nothrow_move_constructible_v<Other>) {
499 emplace<OtherIndex>(other);
500 } else {
501 *this = Variant{other};
502 }
503 };
504 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
505 if (!(((other.activeTypeIndex == Indices) ? (copyAssign(std::in_place_index<Indices>, other.template access<Ts, Indices>()), true) : false) || ...)) {
506 destroy();
507 }
508 }(std::make_index_sequence<sizeof...(Ts)>{});
509 return *this;
510 }
511
513 constexpr Variant& operator=(Variant&& other) noexcept requires(HAS_MOVE_ASSIGNMENT && HAS_TRIVIAL_MOVE_ASSIGNMENT) = default;
514
521 constexpr Variant& operator=(Variant&& other) noexcept(
522 ((std::is_nothrow_move_constructible_v<Ts> && // NOLINT(performance-noexcept-move-constructor, cppcoreguidelines-noexcept-move-operations)
523 std::is_nothrow_move_assignable_v<Ts>) &&
524 ...)) requires(HAS_MOVE_ASSIGNMENT && !HAS_TRIVIAL_MOVE_ASSIGNMENT) {
525 if (this == &other) {
526 return *this;
527 }
528 const auto moveAssign = [&]<size_t OtherIndex, typename Other>(std::in_place_index_t<OtherIndex>, Other& other) -> void {
529 if (activeTypeIndex == OtherIndex) {
530 as<OtherIndex>() = std::move(other);
531 } else {
532 emplace<OtherIndex>(std::move(other));
533 }
534 };
535 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
536 if (!(((other.activeTypeIndex == Indices) ? (moveAssign(std::in_place_index<Indices>, other.template access<Ts, Indices>()), true) : false) || ...)) {
537 destroy();
538 }
539 }(std::make_index_sequence<sizeof...(Ts)>{});
540 return *this;
541 }
542
558 template <typename U>
559 constexpr Variant& operator=(U&& value) noexcept(
560 std::is_nothrow_assignable_v<decltype(F(std::forward<U>(value)))&, U> && std::is_nothrow_constructible_v<decltype(F(std::forward<U>(value))), U>)
561 requires(!std::is_same_v<std::remove_cvref_t<U>, Variant> && variant_has_alternative_v<decltype(F(std::forward<U>(value))), Variant> &&
562 std::is_assignable_v<decltype(F(std::forward<U>(value))), U>) {
563 using T = decltype(F(std::forward<U>(value)));
564 if (is<T>()) {
565 as<T>() = std::forward<U>(value);
566 } else {
567 if constexpr (std::is_nothrow_constructible_v<T, U> || !std::is_nothrow_move_constructible_v<T>) {
568 emplace<T>(std::forward<U>(value));
569 } else {
570 emplace<T>(T(std::forward<U>(value)));
571 }
572 }
573 return *this;
574 }
575
590 template <typename T, typename... Args>
591 constexpr T& emplace(Args&&... args) requires(variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, Args...>) {
592 return emplace<variant_index_v<T, Variant>>(std::forward<Args>(args)...);
593 }
594
611 template <typename T, typename U, typename... Args>
612 constexpr T& emplace(std::initializer_list<U> ilist, Args&&... args)
613 requires(variant_has_alternative_v<T, Variant> && std::is_constructible_v<T, std::initializer_list<U>&, Args...>) {
614 return emplace<variant_index_v<T, Variant>>(ilist, std::forward<Args>(args)...);
615 }
616
631 template <std::size_t Index, typename... Args>
632 constexpr auto& emplace(Args&&... args) requires(std::is_constructible_v<variant_alternative_t<Index, Variant>, Args...>) {
633 static_assert(Index < sizeof...(Ts));
634 destroy();
635 storage.template construct<Index>(std::forward<Args>(args)...);
636 activeTypeIndex = Index;
637 return as<Index>();
638 }
639
656 template <std::size_t Index, typename U, typename... Args>
657 constexpr auto& emplace(std::initializer_list<U> ilist, Args&&... args)
658 requires(std::is_constructible_v<variant_alternative_t<Index, Variant>, std::initializer_list<U>&, Args...>) {
659 static_assert(Index < sizeof...(Ts));
660 destroy();
661 storage.template construct<Index>(ilist, std::forward<Args>(args)...);
662 activeTypeIndex = Index;
663 return as<Index>();
664 }
665
678 constexpr void swap(Variant& other) noexcept(
679 ((std::is_nothrow_move_constructible_v<Ts> && std::is_nothrow_swappable_v<Ts>) && ...)) { // NOLINT(performance-noexcept-swap, cppcoreguidelines-noexcept-swap)
680 if (activeTypeIndex == other.activeTypeIndex) {
681 using std::swap;
682 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
683 (void)(((activeTypeIndex == Indices) ? ((swap(access<Ts, Indices>(), other.template access<Ts, Indices>())), true) : false) || ...);
684 }(std::make_index_sequence<sizeof...(Ts)>{});
685 } else {
686 Variant old = std::move(*this);
687 *this = std::move(other);
688 other = std::move(old);
689 }
690 }
691
705 friend constexpr void swap(Variant& a, Variant& b) noexcept(noexcept(a.swap(b))) { // NOLINT(performance-noexcept-swap, cppcoreguidelines-noexcept-swap)
706 a.swap(b);
707 }
708
716 [[nodiscard]] constexpr index_type index() const noexcept {
717 return activeTypeIndex;
718 }
719
726 [[nodiscard]] constexpr bool valueless_by_exception() const noexcept {
727 return activeTypeIndex == npos;
728 }
729
739 template <typename T>
740 [[nodiscard]] constexpr bool is() const noexcept requires(variant_has_alternative_v<T, Variant>) {
741 return activeTypeIndex == variant_index_v<T, Variant>;
742 }
743
754 template <std::size_t Index>
755 [[nodiscard]] constexpr bool is() const noexcept {
756 return activeTypeIndex == Index;
757 }
758
783 template <typename T>
784 [[nodiscard]] constexpr T& as() & noexcept requires(variant_has_alternative_v<T, Variant>) {
787 }
788
813 template <typename T>
814 [[nodiscard]] constexpr const T& as() const& noexcept requires(variant_has_alternative_v<T, Variant>) {
817 }
818
843 template <typename T>
844 [[nodiscard]] constexpr T&& as() && noexcept requires(variant_has_alternative_v<T, Variant>) {
846 return std::move(as<variant_index_v<T, Variant>>());
847 }
848
873 template <typename T>
874 [[nodiscard]] constexpr const T&& as() const&& noexcept requires(variant_has_alternative_v<T, Variant>) {
876 return std::move(as<variant_index_v<T, Variant>>());
877 }
878
904 template <std::size_t Index>
905 [[nodiscard]] constexpr auto& as() & noexcept {
907 return detail::getUnionMember<Index>(storage);
908 }
909
935 template <std::size_t Index>
936 [[nodiscard]] constexpr const auto& as() const& noexcept {
938 return detail::getUnionMember<Index>(storage);
939 }
940
966 template <std::size_t Index>
967 [[nodiscard]] constexpr auto&& as() && noexcept {
969 return std::move(detail::getUnionMember<Index>(storage));
970 }
971
997 template <std::size_t Index>
998 [[nodiscard]] constexpr const auto&& as() const&& noexcept {
1000 return std::move(detail::getUnionMember<Index>(storage));
1001 }
1002
1017 template <typename T>
1018 [[nodiscard]] constexpr T& get() & requires(variant_has_alternative_v<T, Variant>) {
1019 if (!is<T>()) {
1020 throw BadVariantAccess{};
1021 }
1022 return as<T>();
1023 }
1024
1039 template <typename T>
1040 [[nodiscard]] constexpr const T& get() const& requires(variant_has_alternative_v<T, Variant>) {
1041 if (!is<T>()) {
1042 throw BadVariantAccess{};
1043 }
1044 return as<T>();
1045 }
1046
1061 template <typename T>
1062 [[nodiscard]] constexpr T&& get() && requires(variant_has_alternative_v<T, Variant>) {
1063 if (!is<T>()) {
1064 throw BadVariantAccess{};
1065 }
1066 return std::move(as<T>());
1067 }
1068
1083 template <typename T>
1084 [[nodiscard]] constexpr const T&& get() const&& requires(variant_has_alternative_v<T, Variant>) {
1085 if (!is<T>()) {
1086 throw BadVariantAccess{};
1087 }
1088 return std::move(as<T>());
1089 }
1090
1106 template <std::size_t Index>
1107 [[nodiscard]] constexpr auto& get() & {
1108 if (!is<Index>()) {
1109 throw BadVariantAccess{};
1110 }
1111 return as<Index>();
1112 }
1113
1129 template <std::size_t Index>
1130 [[nodiscard]] constexpr const auto& get() const& {
1131 if (!is<Index>()) {
1132 throw BadVariantAccess{};
1133 }
1134 return as<Index>();
1135 }
1136
1152 template <std::size_t Index>
1153 [[nodiscard]] constexpr auto&& get() && {
1154 if (!is<Index>()) {
1155 throw BadVariantAccess{};
1156 }
1157 return std::move(as<Index>());
1158 }
1159
1175 template <std::size_t Index>
1176 [[nodiscard]] constexpr const auto&& get() const&& {
1177 if (!is<Index>()) {
1178 throw BadVariantAccess{};
1179 }
1180 return std::move(as<Index>());
1181 }
1182
1196 template <typename T>
1197 [[nodiscard]] constexpr T* get_if() noexcept requires(variant_has_alternative_v<T, Variant>) {
1198 return (is<T>()) ? &as<T>() : nullptr;
1199 }
1200
1215 template <typename T>
1216 [[nodiscard]] constexpr const T* get_if() const noexcept requires(variant_has_alternative_v<T, Variant>) {
1217 return (is<T>()) ? &as<T>() : nullptr;
1218 }
1219
1233 template <std::size_t Index>
1234 [[nodiscard]] constexpr auto* get_if() noexcept {
1235 return (is<Index>()) ? &as<Index>() : nullptr;
1236 }
1237
1252 template <std::size_t Index>
1253 [[nodiscard]] constexpr const auto* get_if() const noexcept {
1254 return (is<Index>()) ? &as<Index>() : nullptr;
1255 }
1256
1274 template <typename Visitor, typename V>
1275 static constexpr decltype(auto) visit(Visitor&& visitor, V&& variant) { // NOLINT(cppcoreguidelines-missing-std-forward)
1276 using R = std::common_reference_t<decltype(visitor(detail::forwardLike<V>(std::declval<Ts>())))...>;
1277 if constexpr (std::is_void_v<R>) {
1278 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
1279 if (!(((variant.activeTypeIndex == Indices) ? (visitor(std::forward<V>(variant).template access<Ts, Indices>()), true) : false) || ...)) {
1280 throw BadVariantAccess{};
1281 }
1282 }(std::make_index_sequence<sizeof...(Ts)>{});
1283 } else if constexpr (std::is_lvalue_reference_v<R>) {
1284 std::remove_reference_t<R>* result = nullptr;
1285 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
1286 if (!(((variant.activeTypeIndex == Indices) ? ((result = &visitor(std::forward<V>(variant).template access<Ts, Indices>())), true) : false) || ...)) {
1287 throw BadVariantAccess{};
1288 }
1289 }(std::make_index_sequence<sizeof...(Ts)>{});
1290 return *result;
1291 } else {
1292 struct Result {
1293 union MaybeValue {
1294 R actual;
1295 char dummy{};
1296
1297 constexpr ~MaybeValue() {}
1298 };
1299
1300 MaybeValue value{};
1301 bool hasValue = false;
1302
1303 constexpr ~Result() {
1304 if (hasValue) {
1305 std::destroy_at(&value.actual);
1306 }
1307 }
1308 } result{};
1309 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
1310 if (!(((variant.activeTypeIndex == Indices) ? (std::construct_at(&result.value.actual, visitor(std::forward<V>(variant).template access<Ts, Indices>())), true)
1311 : false) ||
1312 ...)) {
1313 throw BadVariantAccess{};
1314 }
1315 }(std::make_index_sequence<sizeof...(Ts)>{});
1316 result.hasValue = true;
1317 return R{std::move(result.value.actual)};
1318 }
1319 }
1320
1337 template <typename Visitor>
1338 static constexpr decltype(auto) visitIndex(index_type index, Visitor&& visitor) { // NOLINT(cppcoreguidelines-missing-std-forward)
1339 using R = std::common_reference_t<decltype(visitor(std::in_place_type<Ts>))...>;
1340 if constexpr (std::is_void_v<R>) {
1341 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
1342 if (!(((index == Indices) ? (visitor(std::in_place_type<Ts>), true) : false) || ...)) {
1343 throw BadVariantAccess{};
1344 }
1345 }(std::make_index_sequence<sizeof...(Ts)>{});
1346 } else if constexpr (std::is_lvalue_reference_v<R>) {
1347 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
1348 std::remove_reference_t<R>* result = nullptr;
1349 if (!(((index == Indices) ? ((result = &visitor(std::in_place_type<Ts>)), true) : false) || ...)) {
1350 throw BadVariantAccess{};
1351 }
1352 return *result;
1353 }(std::make_index_sequence<sizeof...(Ts)>{});
1354 } else {
1355 struct Result {
1356 union MaybeValue {
1357 R actual;
1358 char dummy{};
1359
1360 constexpr ~MaybeValue() {}
1361 };
1362
1363 MaybeValue value{};
1364 bool hasValue = false;
1365
1366 constexpr ~Result() {
1367 if (hasValue) {
1368 std::destroy_at(&value.actual);
1369 }
1370 }
1371 } result{};
1372 [&]<std::size_t... Indices>(std::index_sequence<Indices...>) -> void {
1373 if (!(((index == Indices) ? (std::construct_at(&result.value.actual, visitor(std::in_place_type<Ts>)), true) : false) || ...)) {
1374 throw BadVariantAccess{};
1375 }
1376 }(std::make_index_sequence<sizeof...(Ts)>{});
1377 result.hasValue = true;
1378 return R{std::move(result.value.actual)};
1379 }
1380 }
1381
1395 template <typename Function>
1396 static constexpr decltype(auto) apply(Function&& function) {
1397 return std::forward<Function>(function)(std::in_place_type<Ts>...);
1398 }
1399
1400private:
1401 template <typename... Us>
1402 friend class Variant;
1403
1404 void destroy() noexcept {
1405 if constexpr (!HAS_TRIVIAL_DESTRUCTOR) {
1406 storage.destroy(activeTypeIndex);
1407 }
1408 activeTypeIndex = npos;
1409 }
1410
1411 template <typename T, size_t Index>
1412 [[nodiscard]] constexpr T& access() & noexcept {
1413 if (std::is_constant_evaluated()) {
1414 return detail::getUnionMember<Index>(storage);
1415 }
1416 return *std::launder(reinterpret_cast<T*>(&storage));
1417 }
1418
1419 template <typename T, size_t Index>
1420 [[nodiscard]] constexpr const T& access() const& noexcept {
1421 if (std::is_constant_evaluated()) {
1422 return detail::getUnionMember<Index>(storage);
1423 }
1424 return *std::launder(reinterpret_cast<const T*>(&storage));
1425 }
1426
1427 template <typename T, size_t Index>
1428 [[nodiscard]] constexpr T&& access() && noexcept {
1429 if (std::is_constant_evaluated()) {
1430 return std::move(detail::getUnionMember<Index>(storage));
1431 }
1432 return std::move(*std::launder(reinterpret_cast<T*>(&storage)));
1433 }
1434
1435 template <typename T, size_t Index>
1436 [[nodiscard]] constexpr const T&& access() const&& noexcept {
1437 if (std::is_constant_evaluated()) {
1438 return std::move(detail::getUnionMember<Index>(storage));
1439 }
1440 return std::move(*std::launder(reinterpret_cast<const T*>(&storage)));
1441 }
1442
1443 detail::UnionStorage<Ts..., Monostate> storage;
1444 index_type activeTypeIndex;
1445};
1446
1464template <typename Visitor, typename V>
1465constexpr decltype(auto) visit(Visitor&& visitor, V&& variant) requires(requires { typename std::remove_reference_t<V>::GREM_private_DerivedFromVariantTag; }) {
1466 return std::remove_reference_t<V>::visit(std::forward<Visitor>(visitor), std::forward<V>(variant));
1467}
1468
1482template <typename T, typename... Ts>
1483[[nodiscard]] constexpr bool holds_alternative(const Variant<Ts...>& variant) noexcept {
1484 return variant.template is<T>();
1485}
1486
1500template <std::size_t Index, typename... Ts>
1501[[nodiscard]] constexpr bool holds_alternative(const Variant<Ts...>& variant) noexcept {
1502 return variant.template is<Index>();
1503}
1504
1520template <typename T, typename... Ts>
1521[[nodiscard]] constexpr T& get(Variant<Ts...>& variant) {
1522 return variant.template get<T>();
1523}
1524
1540template <typename T, typename... Ts>
1541[[nodiscard]] constexpr const T& get(const Variant<Ts...>& variant) {
1542 return variant.template get<T>();
1543}
1544
1560template <std::size_t Index, typename... Ts>
1561[[nodiscard]] constexpr auto& get(Variant<Ts...>& variant) {
1562 return variant.template get<Index>();
1563}
1564
1580template <std::size_t Index, typename... Ts>
1581[[nodiscard]] constexpr const auto& get(const Variant<Ts...>& variant) {
1582 return variant.template get<Index>();
1583}
1584
1600template <typename T, typename... Ts>
1601[[nodiscard]] constexpr T* get_if(Variant<Ts...>* variant) noexcept {
1602 GREM_ASSERT(variant);
1603 return variant->template get_if<T>();
1604}
1605
1622template <typename T, typename... Ts>
1623[[nodiscard]] constexpr const T* get_if(const Variant<Ts...>* variant) noexcept {
1624 GREM_ASSERT(variant);
1625 return variant->template get_if<T>();
1626}
1627
1643template <std::size_t Index, typename... Ts>
1644[[nodiscard]] constexpr auto* get_if(Variant<Ts...>* variant) noexcept {
1645 GREM_ASSERT(variant);
1646 return variant->template get_if<Index>();
1647}
1648
1664template <std::size_t Index, typename... Ts>
1665[[nodiscard]] constexpr const auto* get_if(const Variant<Ts...>* variant) noexcept {
1666 GREM_ASSERT(variant);
1667 return variant->template get_if<Index>();
1668}
1669
1682template <typename... Ts>
1683[[nodiscard]] constexpr bool operator==(const Variant<Ts...>& a, const Variant<Ts...>& b) requires(requires {
1684 { a.template as<Ts>() == b.template as<Ts>() } -> detail::bool_convertible;
1685} && ...) {
1686 if (a.index() != b.index()) {
1687 return false;
1688 }
1689 if (a.valueless_by_exception()) {
1690 return true;
1691 }
1692 return visit([&]<typename T>(const T& value) -> bool { return value == b.template as<T>(); }, a);
1693}
1694
1707template <typename... Ts>
1708[[nodiscard]] constexpr bool operator!=(const Variant<Ts...>& a, const Variant<Ts...>& b) requires(requires {
1709 { a.template as<Ts>() != b.template as<Ts>() } -> detail::bool_convertible;
1710} && ...) {
1711 if (a.index() != b.index()) {
1712 return true;
1713 }
1714 if (a.valueless_by_exception()) {
1715 return false;
1716 }
1717 return visit([&]<typename T>(const T& value) -> bool { return value != b.template as<T>(); }, a);
1718}
1719
1732template <typename... Ts>
1733[[nodiscard]] constexpr bool operator<(const Variant<Ts...>& a, const Variant<Ts...>& b) requires(requires {
1734 { a.template as<Ts>() < b.template as<Ts>() } -> detail::bool_convertible;
1735} && ...) {
1736 if (a.valueless_by_exception() && b.valueless_by_exception()) {
1737 return false;
1738 }
1739 if (a.valueless_by_exception()) {
1740 return true;
1741 }
1742 if (b.valueless_by_exception()) {
1743 return false;
1744 }
1745 if (a.index() != b.index()) {
1746 return a.index() < b.index();
1747 }
1748 return visit([&]<typename T>(const T& value) -> bool { return value < b.template as<T>(); }, a);
1749}
1750
1763template <typename... Ts>
1764[[nodiscard]] constexpr bool operator<=(const Variant<Ts...>& a, const Variant<Ts...>& b) requires(requires {
1765 { a.template as<Ts>() <= b.template as<Ts>() } -> detail::bool_convertible;
1766} && ...) {
1767 if (a.valueless_by_exception() && b.valueless_by_exception()) {
1768 return true;
1769 }
1770 if (a.valueless_by_exception()) {
1771 return true;
1772 }
1773 if (b.valueless_by_exception()) {
1774 return false;
1775 }
1776 if (a.index() != b.index()) {
1777 return a.index() <= b.index();
1778 }
1779 return visit([&]<typename T>(const T& value) -> bool { return value <= b.template as<T>(); }, a);
1780}
1781
1794template <typename... Ts>
1795[[nodiscard]] constexpr bool operator>(const Variant<Ts...>& a, const Variant<Ts...>& b) requires(requires {
1796 { a.template as<Ts>() > b.template as<Ts>() } -> detail::bool_convertible;
1797} && ...) {
1798 if (a.valueless_by_exception() && b.valueless_by_exception()) {
1799 return false;
1800 }
1801 if (a.valueless_by_exception()) {
1802 return false;
1803 }
1804 if (b.valueless_by_exception()) {
1805 return true;
1806 }
1807 if (a.index() != b.index()) {
1808 return a.index() > b.index();
1809 }
1810 return visit([&]<typename T>(const T& value) -> bool { return value > b.template as<T>(); }, a);
1811}
1812
1825template <typename... Ts>
1826[[nodiscard]] constexpr bool operator>=(const Variant<Ts...>& a, const Variant<Ts...>& b) requires(requires {
1827 { a.template as<Ts>() >= b.template as<Ts>() } -> detail::bool_convertible;
1828} && ...) {
1829 if (a.valueless_by_exception() && b.valueless_by_exception()) {
1830 return true;
1831 }
1832 if (a.valueless_by_exception()) {
1833 return false;
1834 }
1835 if (b.valueless_by_exception()) {
1836 return true;
1837 }
1838 if (a.index() != b.index()) {
1839 return a.index() >= b.index();
1840 }
1841 return visit([&]<typename T>(const T& value) -> bool { return value >= b.template as<T>(); }, a);
1842}
1843
1856template <typename... Ts>
1857[[nodiscard]] constexpr std::common_comparison_category_t<std::compare_three_way_result_t<Ts>...> operator<=>(const Variant<Ts...>& a, const Variant<Ts...>& b) {
1859 return std::strong_ordering::equal;
1860 }
1861 if (a.valueless_by_exception()) {
1862 return std::strong_ordering::less;
1863 }
1864 if (b.valueless_by_exception()) {
1865 return std::strong_ordering::greater;
1866 }
1867 if (a.index() != b.index()) {
1868 return a.index() <=> b.index();
1869 }
1870 return visit([&]<typename T>(const T& value) -> std::common_comparison_category_t<std::compare_three_way_result_t<Ts>...> { return value <=> b.template as<T>(); }, a);
1871}
1872
1879template <typename... Functors>
1880struct Overloaded : Functors... {
1881 using Functors::operator()...;
1882};
1883
1884template <typename... Functors>
1885Overloaded(Functors...) -> Overloaded<Functors...>;
1886
1887namespace detail {
1888
1889template <typename V>
1890struct Matcher {
1891 V variant;
1892
1893 template <typename... Functors>
1894 constexpr decltype(auto) operator()(Functors&&... functors) const {
1895 return visit(Overloaded{std::forward<Functors>(functors)...}, variant);
1896 }
1897};
1898
1899} // namespace detail
1900
1921template <typename V>
1922[[nodiscard]] constexpr detail::Matcher<V> match(V&& variant) {
1923 return detail::Matcher<V>{std::forward<V>(variant)};
1924}
1925
1926} // namespace grem
1927
1928template <typename... Ts>
1929struct std::hash<grem::Variant<Ts...>> {
1930 [[nodiscard]] std::size_t operator()(const grem::Variant<Ts...>& variant) const {
1931 return visit(hasher, variant);
1932 }
1933
1934private:
1935 template <typename T>
1936 class Hash {
1937 public:
1938 [[nodiscard]] std::size_t operator()(const T& value) const {
1939 return hasher(value);
1940 }
1941
1942 private:
1943 [[no_unique_address]] std::hash<T> hasher;
1944 };
1945
1946 class HashVisitor : public Hash<Ts>... {
1947 public:
1948 using Hash<Ts>::operator()...;
1949 };
1950
1951 [[no_unique_address]] HashVisitor hasher;
1952};
1953
1954namespace grem {
1955namespace detail {
1956
1957template <typename Signature>
1958struct match_case_type;
1959
1960template <typename Arg>
1961struct match_case_type<void(Arg)> {
1962 using type = Arg;
1963};
1964
1965template <typename Arg>
1966struct match_case_type<void(Arg&)> {
1967 using type = Arg;
1968};
1969
1970template <typename Arg>
1971struct match_case_type<void(Arg&&)> {
1972 using type = Arg;
1973};
1974
1975template <typename Arg>
1976struct match_case_type<void(const Arg&)> {
1977 using type = Arg;
1978};
1979
1980template <typename Arg>
1981struct match_case_type<void(const Arg&&)> {
1982 using type = Arg;
1983};
1984
1985template <typename Signature>
1986using match_case_type_t = typename match_case_type<Signature>::type;
1987
1988template <typename... Ts>
1989consteval Variant<Ts...> matchVariantType(const Variant<Ts...>&);
1990
1991template <typename T>
1992consteval typename T::value_type matchVariantType(const T&);
1993
1994} // namespace detail
1995} // namespace grem
1996
1997#define GREM_MATCH(...) switch (auto&& GREM_private_matchVariant = __VA_ARGS__; GREM_private_matchVariant.index())
1998#define GREM_CASE(...) \
1999 case grem::variant_index_v<grem::detail::match_case_type_t<void(__VA_ARGS__)>, decltype(grem::detail::matchVariantType(GREM_private_matchVariant))>: \
2000 if ([[maybe_unused]] __VA_ARGS__ = GREM_private_matchVariant.template as<grem::detail::match_case_type_t<void(__VA_ARGS__)>>(); true)
2001#define GREM_CASE_DEFAULT(...) \
2002 default: \
2003 if ([[maybe_unused]] __VA_ARGS__ = GREM_private_matchVariant; true)
2004
2005#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Tagged union value type that holds a value of one of the given types.
Definition Variant.hpp:293
constexpr const T * get_if() const noexcept
Access the underlying value with the given type if it is the currently active alternative.
Definition Variant.hpp:1216
constexpr Variant(std::in_place_index_t< Index > index, std::initializer_list< U > ilist, Args &&... args)
Construct a variant alternative in-place given its index, with an initializer list as the first const...
Definition Variant.hpp:445
constexpr const auto & get() const &
Access the underlying value with the given index.
Definition Variant.hpp:1130
constexpr ~Variant()=default
Trivial destructor.
constexpr Variant(Variant &&other) noexcept((std::is_nothrow_move_constructible_v< Ts > &&...))
Move constructor.
Definition Variant.hpp:473
constexpr auto && get() &&
Access the underlying value with the given index.
Definition Variant.hpp:1153
constexpr const T & as() const &noexcept
Access the underlying value with the given type without a safety check.
Definition Variant.hpp:814
constexpr Variant & operator=(const Variant &other)=default
Trivial copy assignment.
constexpr Variant & operator=(Variant &&other) noexcept=default
Trivial move assignment.
constexpr Variant & operator=(Variant &&other) noexcept(((std::is_nothrow_move_constructible_v< Ts > &&//NOLINT(performance-noexcept-move-constructor, cppcoreguidelines-noexcept-move-operations) std::is_nothrow_move_assignable_v< Ts >) &&...))
Move assignment.
Definition Variant.hpp:521
constexpr Variant(std::in_place_type_t< T > type, std::initializer_list< U > ilist, Args &&... args)
Construct a variant alternative in-place given its type, with an initializer list as the first constr...
Definition Variant.hpp:406
constexpr Variant & operator=(U &&value) noexcept(std::is_nothrow_assignable_v< decltype(F(std::forward< U >(value)))&, U > &&std::is_nothrow_constructible_v< decltype(F(std::forward< U >(value))), U >)
Converting assignment.
Definition Variant.hpp:559
constexpr const T && get() const &&
Access the underlying value with the given type.
Definition Variant.hpp:1084
constexpr auto * get_if() noexcept
Access the underlying value with the given index if it is the currently active alternative.
Definition Variant.hpp:1234
constexpr void swap(Variant &other) noexcept(((std::is_nothrow_move_constructible_v< Ts > &&std::is_nothrow_swappable_v< Ts >) &&...))
Swap this variant's value with that of another.
Definition Variant.hpp:678
constexpr T && get() &&
Access the underlying value with the given type.
Definition Variant.hpp:1062
constexpr T & emplace(Args &&... args)
Construct an alternative given its type, destroying the old value.
Definition Variant.hpp:591
constexpr const T && as() const &&noexcept
Access the underlying value with the given type without a safety check.
Definition Variant.hpp:874
friend class Variant
JSON value type using the default allocator.
Definition Variant.hpp:1402
static constexpr decltype(auto) visit(Visitor &&visitor, V &&variant)
Call a visitor functor with the currently active underlying value of a variant.
Definition Variant.hpp:1275
static constexpr index_type npos
Definition Variant.hpp:342
constexpr ~Variant()
Destructor.
Definition Variant.hpp:451
constexpr auto & as() &noexcept
Access the underlying value with the given index without a safety check.
Definition Variant.hpp:905
constexpr const auto && get() const &&
Access the underlying value with the given index.
Definition Variant.hpp:1176
constexpr T * get_if() noexcept
Access the underlying value with the given type if it is the currently active alternative.
Definition Variant.hpp:1197
constexpr const auto & as() const &noexcept
Access the underlying value with the given index without a safety check.
Definition Variant.hpp:936
friend constexpr void swap(Variant &a, Variant &b) noexcept(noexcept(a.swap(b)))
Swap the values of two variants.
Definition Variant.hpp:705
constexpr Variant() noexcept(std::is_nothrow_default_constructible_v< variant_alternative_t< 0, Variant > >)
Default-construct a variant with the first variant alternative, if it is default-constructible.
Definition Variant.hpp:351
constexpr Variant & operator=(const Variant &other)=delete
Deleted copy assignment.
constexpr Variant(U &&value) noexcept(std::is_nothrow_constructible_v< decltype(F(std::forward< U >(value))), U >)
Converting constructor.
Definition Variant.hpp:368
constexpr T && as() &&noexcept
Access the underlying value with the given type without a safety check.
Definition Variant.hpp:844
constexpr Variant(std::in_place_index_t< Index > index, Args &&... args)
Construct a variant alternative in-place given its index.
Definition Variant.hpp:424
constexpr Variant(std::in_place_type_t< T > type, Args &&... args)
Construct a variant alternative in-place given its type.
Definition Variant.hpp:385
constexpr Variant(const Variant &other)=default
Trivial copy constructor.
constexpr T & emplace(std::initializer_list< U > ilist, Args &&... args)
Construct an alternative given its type, with an initializer list as the first constructor argument,...
Definition Variant.hpp:612
constexpr index_type index() const noexcept
Definition Variant.hpp:716
std::conditional_t< sizeof...(Ts)< 255ull, std::uint8_t, std::conditional_t< sizeof...(Ts)< 65535ull, std::uint16_t, std::conditional_t< sizeof...(Ts)< 4294967295ull, std::uint32_t, std::uint64_t > > > index_type
Index type used to encode the active alternative type.
Definition Variant.hpp:332
constexpr Variant(const Variant &other)
Copy constructor.
Definition Variant.hpp:465
constexpr bool is() const noexcept
Definition Variant.hpp:740
constexpr auto && as() &&noexcept
Access the underlying value with the given index without a safety check.
Definition Variant.hpp:967
constexpr Variant(const Variant &other)=delete
Deleted copy constructor.
static constexpr decltype(auto) apply(Function &&function)
Call a functor with all alternative types.
Definition Variant.hpp:1396
static constexpr decltype(auto) visitIndex(index_type index, Visitor &&visitor)
Call a visitor functor with the type corresponding to a variant index.
Definition Variant.hpp:1338
constexpr const auto * get_if() const noexcept
Access the underlying value with the given index if it is the currently active alternative.
Definition Variant.hpp:1253
constexpr bool is() const noexcept
Check if the variant currently holds the alternative with the given index.
Definition Variant.hpp:755
constexpr Variant(Variant &&other) noexcept=default
Trivial move constructor.
constexpr T & get() &
Access the underlying value with the given type.
Definition Variant.hpp:1018
constexpr const auto && as() const &&noexcept
Access the underlying value with the given index without a safety check.
Definition Variant.hpp:998
constexpr auto & emplace(Args &&... args)
Construct an alternative given its index, destroying the old value.
Definition Variant.hpp:632
constexpr auto & get() &
Access the underlying value with the given index.
Definition Variant.hpp:1107
constexpr const T & get() const &
Access the underlying value with the given type.
Definition Variant.hpp:1040
constexpr T & as() &noexcept
Access the underlying value with the given type without a safety check.
Definition Variant.hpp:784
constexpr Variant & operator=(const Variant &other)
Copy assignment.
Definition Variant.hpp:491
constexpr bool valueless_by_exception() const noexcept
Check if the variant is in the valueless by exception state.
Definition Variant.hpp:726
constexpr auto & emplace(std::initializer_list< U > ilist, Args &&... args)
Construct an alternative given its index, with an initializer list as the first constructor argument,...
Definition Variant.hpp:657
@ U
'u'
Definition keyboard.hpp:91
@ N
'n'
Definition keyboard.hpp:84
@ T
't'
Definition keyboard.hpp:90
json::VariantBase< std::allocator > Variant
JSON value type using the default allocator.
Definition json.hpp:1094
Definition aliases.hpp:9
std::function< Signature > Function
Definition Function.hpp:14
constexpr tuple_element_t< Index, Pair< T1, T2 > > & get(Pair< T1, T2 > &t) noexcept
Definition Pair.hpp:202
constexpr bool variant_has_alternative_v
Check if a variant type has a given type as one of its possible alternatives.
Definition Variant.hpp:179
constexpr std::size_t variant_size_v
Get the number of alternative types of a variant type.
Definition Variant.hpp:234
typename variant_alternative< Index, V >::type variant_alternative_t
Get the type of the variant alternative with a given index in a variant type.
Definition Variant.hpp:218
constexpr T * get_if(Variant< Ts... > *variant) noexcept
Access the underlying value with the given type of a variant if it is the currently active alternativ...
Definition Variant.hpp:1601
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
constexpr decltype(auto) visit(Visitor &&visitor, V &&variant)
Call a visitor functor with the currently active underlying value of a variant.
Definition Variant.hpp:1465
constexpr bool operator<(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:324
constexpr detail::Matcher< V > match(V &&variant)
Choose a function overload to execute based on the active alternative of a variant.
Definition Variant.hpp:1922
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
constexpr bool holds_alternative(const Variant< Ts... > &variant) noexcept
Check if a variant currently holds the alternative with the given type.
Definition Variant.hpp:1483
constexpr bool operator<=(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:329
constexpr bool operator>=(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:339
constexpr std::size_t variant_index_v
Get the index of the variant alternative with a given type in a variant type.
Definition Variant.hpp:196
constexpr bool operator!=(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:319
constexpr bool operator>(Span< T, N > a, Span< T, M > b)
Definition Span.hpp:334
Overloaded(Functors...) -> Overloaded< Functors... >
Exception type that is thrown on an attempt to erroneously access an inactive alternative of a Varian...
Definition Variant.hpp:284
const char * what() const noexcept override
Definition Variant.hpp:287
BadVariantAccess() noexcept=default
Unit type for representing an empty alternative in Variant.
Definition Variant.hpp:242
Visitor type for combining callable objects, such as lambdas, into an overload set.
Definition Variant.hpp:1880
std::size_t operator()(const grem::Monostate &) const
Definition Variant.hpp:272
std::size_t operator()(const grem::Variant< Ts... > &variant) const
Definition Variant.hpp:1930