GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Optional.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_OPTIONAL_HPP
5#define GREM_CORE_DATA_OPTIONAL_HPP
6
8
11
12#include <compare> // std::strong_ordering, std::compare_three_way_result_t
13#include <cstddef> // std::size_t
14#include <exception> // std::exception
15#include <functional> // std::hash, std::invoke
16#include <initializer_list> // std::initializer_list
17#include <type_traits> // std::is_..._v, std::remove_..._t, std::decay_t, std::invoke_result_t
18#include <utility> // std::move, std::forward, std::in_place...
19
20namespace grem {
21
25struct Nullopt {
29 constexpr explicit Nullopt(int) {}
30};
31
35inline constexpr Nullopt nullopt{0};
36
41struct BadOptionalAccess : std::exception {
42 BadOptionalAccess() noexcept = default;
43
44 [[nodiscard]] const char* what() const noexcept override {
45 return "Bad optional access.";
46 }
47};
48
57template <typename T>
58class Optional {
59public:
63 constexpr Optional() noexcept {} // NOLINT(modernize-use-equals-default)
64
70 constexpr Optional(Nullopt value) noexcept {
71 (void)value;
72 }
73
83 template <typename... Args>
84 constexpr explicit Optional(std::in_place_t tag, Args&&... args) requires(std::is_constructible_v<T, Args...>)
85 : variant(std::in_place_type<T>, std::forward<Args>(args)...) {
86 (void)tag;
87 }
88
102 template <typename U, typename... Args>
103 constexpr Optional(std::in_place_t tag, std::initializer_list<U> ilist, Args&&... args) requires(std::is_constructible_v<T, std::initializer_list<U>&, Args...>)
104 : variant(std::in_place_type<T>, ilist, std::forward<Args>(args)...) {
105 (void)tag;
106 }
107
116 template <typename U = std::remove_cv_t<T>>
117 constexpr Optional(U&& value)
118 requires(!std::is_same_v<std::remove_cvref_t<U>, std::in_place_t> && !std::is_same_v<std::remove_cvref_t<U>, Optional> &&
119 (!std::is_same_v<std::remove_cv_t<T>, bool> || !template_specialization_of<std::remove_cvref_t<U>, Optional>) && std::is_constructible_v<T, U &&> &&
120 std::is_convertible_v<U &&, T>)
121 : variant(std::in_place_type<T>, std::forward<U>(value)) {}
122
131 template <typename U = std::remove_cv_t<T>>
132 constexpr explicit Optional(U&& value)
133 requires(!std::is_same_v<std::remove_cvref_t<U>, std::in_place_t> && !std::is_same_v<std::remove_cvref_t<U>, Optional> &&
134 (!std::is_same_v<std::remove_cv_t<T>, bool> || !template_specialization_of<std::remove_cvref_t<U>, Optional>) && std::is_constructible_v<T, U &&> &&
135 !std::is_convertible_v<U &&, T>)
136 : variant(std::in_place_type<T>, std::forward<U>(value)) {}
137
139 constexpr ~Optional() = default;
140
142 constexpr Optional(const Optional& other) = default;
143
145 constexpr Optional(Optional&& other) noexcept(
146 std::is_nothrow_move_constructible_v<T>) = default; // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
147
149 template <typename U>
150 constexpr Optional(const Optional<U>& other)
151 requires((std::is_same_v<std::remove_cv_t<T>, bool> ||
152 (!std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> &&
153 !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> &&
154 !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T>)) &&
155 std::is_constructible_v<T, const U&> && std::is_convertible_v<const U&, T>)
156 : Optional() {
157 if (other) {
158 emplace(*other);
159 }
160 }
161
163 template <typename U>
164 constexpr explicit Optional(const Optional<U>& other)
165 requires((std::is_same_v<std::remove_cv_t<T>, bool> ||
166 (!std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> &&
167 !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> &&
168 !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T>)) &&
169 std::is_constructible_v<T, const U&> && !std::is_convertible_v<const U&, T>)
170 : Optional() {
171 if (other) {
172 emplace(*other);
173 }
174 }
175
177 template <typename U>
178 constexpr Optional(Optional<U>&& other) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
179 requires((std::is_same_v<std::remove_cv_t<T>, bool> ||
180 (!std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> &&
181 !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> &&
182 !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T>)) &&
183 std::is_constructible_v<T, U &&> && std::is_convertible_v<U &&, T>)
184 : Optional() {
185 if (other) {
186 emplace(std::move(*other));
187 }
188 }
189
191 template <typename U>
192 constexpr explicit Optional(Optional<U>&& other) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
193 requires((std::is_same_v<std::remove_cv_t<T>, bool> ||
194 (!std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> &&
195 !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> &&
196 !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T>)) &&
197 std::is_constructible_v<T, U &&> && !std::is_convertible_v<U &&, T>)
198 : Optional() {
199 if (other) {
200 emplace(std::move(*other));
201 }
202 }
203
208 constexpr Optional& operator=(Nullopt) noexcept {
209 reset();
210 return *this;
211 }
212
222 template <typename U = std::remove_cv_t<T>>
223 constexpr Optional& operator=(U&& value)
224 requires(!std::is_same_v<std::remove_cvref_t<U>, Optional> && std::is_constructible_v<T, U> && std::is_assignable_v<T&, U> &&
225 (!std::is_scalar_v<T> || !std::is_same_v<T, std::decay_t<U>>)) {
226 if (*this) {
227#ifdef _MSC_VER
228#pragma warning(push)
229#pragma warning(disable : 4267)
230#elif defined(__clang__)
231#pragma clang diagnostic push
232#pragma clang diagnostic ignored "-Wshorten-64-to-32"
233#pragma clang diagnostic ignored "-Wfloat-conversion"
234#elif defined(__GNUC__)
235#pragma GCC diagnostic push
236#pragma GCC diagnostic ignored "-Wconversion"
237#pragma GCC diagnostic ignored "-Wfloat-conversion"
238#endif
239 **this = std::forward<U>(value);
240#ifdef _MSC_VER
241#pragma warning(pop)
242#elif defined(__clang__)
243#pragma clang diagnostic pop
244#elif defined(__GNUC__)
245#pragma GCC diagnostic pop
246#endif
247 } else {
248 emplace(std::forward<U>(value));
249 }
250 return *this;
251 }
252
254 constexpr Optional& operator=(const Optional& other) = default;
255
257 constexpr Optional& operator=(Optional&& other) noexcept(
258 std::is_nothrow_move_constructible_v<T> && // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
259 std::is_nothrow_move_assignable_v<T>) = default;
260
262 template <typename U>
263 constexpr Optional& operator=(const Optional<U>& other)
264 requires(std::is_constructible_v<T, const U&> && std::is_assignable_v<T&, const U&> && !std::is_constructible_v<T, Optional<U>&> &&
265 !std::is_constructible_v<T, const Optional<U>&> && !std::is_constructible_v<T, Optional<U> &&> && !std::is_constructible_v<T, const Optional<U> &&> &&
266 !std::is_convertible_v<Optional<U>&, T> && !std::is_convertible_v<const Optional<U>&, T> && !std::is_convertible_v<Optional<U> &&, T> &&
267 !std::is_convertible_v<const Optional<U> &&, T> && !std::is_assignable_v<T&, Optional<U>&> && !std::is_assignable_v<T&, const Optional<U>&> &&
268 !std::is_assignable_v<T&, Optional<U> &&> && !std::is_assignable_v<T&, const Optional<U> &&>) {
269 if (*this) {
270 if (other) {
271 **this = **other;
272 } else {
273 reset();
274 }
275 } else if (other) {
276 emplace(*other);
277 }
278 return *this;
279 }
280
282 template <typename U>
283 constexpr Optional& operator=(Optional<U>&& other) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
284 requires(std::is_constructible_v<T, U> && std::is_assignable_v<T&, U> && !std::is_constructible_v<T, Optional<U>&> && !std::is_constructible_v<T, const Optional<U>&> &&
285 !std::is_constructible_v<T, Optional<U> &&> && !std::is_constructible_v<T, const Optional<U> &&> && !std::is_convertible_v<Optional<U>&, T> &&
286 !std::is_convertible_v<const Optional<U>&, T> && !std::is_convertible_v<Optional<U> &&, T> && !std::is_convertible_v<const Optional<U> &&, T> &&
287 !std::is_assignable_v<T&, Optional<U>&> && !std::is_assignable_v<T&, const Optional<U>&> && !std::is_assignable_v<T&, Optional<U> &&> &&
288 !std::is_assignable_v<T&, const Optional<U> &&>) {
289 if (*this) {
290 if (other) {
291 **this = std::move(**other);
292 } else {
293 reset();
294 }
295 } else if (other) {
296 emplace(std::move(*other));
297 }
298 return *this;
299 }
300
308 [[nodiscard]] constexpr const T* operator->() const noexcept {
309 return &**this;
310 }
311
319 [[nodiscard]] constexpr T* operator->() noexcept {
320 return &**this;
321 }
322
330 [[nodiscard]] constexpr const T& operator*() const& noexcept {
331 return variant.template as<T>();
332 }
333
341 [[nodiscard]] constexpr T& operator*() & noexcept {
342 return variant.template as<T>();
343 }
344
352 [[nodiscard]] constexpr const T&& operator*() const&& noexcept {
353 return std::move(variant.template as<T>());
354 }
355
363 [[nodiscard]] constexpr T&& operator*() && noexcept {
364 return std::move(variant.template as<T>());
365 }
366
373 constexpr explicit operator bool() const noexcept {
374 return has_value();
375 }
376
383 [[nodiscard]] constexpr bool has_value() const noexcept {
384 return variant.template is<T>();
385 }
386
394 [[nodiscard]] constexpr T& value() & {
395 if (!*this) {
396 throw BadOptionalAccess{};
397 }
398 return **this;
399 }
400
408 [[nodiscard]] constexpr const T& value() const& {
409 if (!*this) {
410 throw BadOptionalAccess{};
411 }
412 return **this;
413 }
414
422 [[nodiscard]] constexpr T&& value() && {
423 if (!*this) {
424 throw BadOptionalAccess{};
425 }
426 return std::move(**this);
427 }
428
436 [[nodiscard]] constexpr const T&& value() const&& {
437 if (!*this) {
438 throw BadOptionalAccess{};
439 }
440 return std::move(**this);
441 }
442
452 template <typename U>
453 [[nodiscard]] constexpr T value_or(U&& defaultValue) const& {
454 return (*this) ? **this : static_cast<T>(std::forward<U>(defaultValue));
455 }
456
466 template <typename U>
467 [[nodiscard]] constexpr T value_or(U&& defaultValue) && {
468 return (*this) ? std::move(**this) : static_cast<T>(std::forward<U>(defaultValue));
469 }
470
483 template <typename F>
484 constexpr auto and_then(F&& f) & {
485 if (*this) {
486 return std::invoke(std::forward<F>(f), **this);
487 }
488 return std::remove_cvref_t<std::invoke_result_t<F, T&>>{};
489 }
490
503 template <typename F>
504 constexpr auto and_then(F&& f) const& {
505 if (*this) {
506 return std::invoke(std::forward<F>(f), **this);
507 }
508 return std::remove_cvref_t<std::invoke_result_t<F, const T&>>{};
509 }
510
523 template <typename F>
524 constexpr auto and_then(F&& f) && {
525 if (*this) {
526 return std::invoke(std::forward<F>(f), std::move(**this));
527 }
528 return std::remove_cvref_t<std::invoke_result_t<F, T>>{};
529 }
530
543 template <typename F>
544 constexpr auto and_then(F&& f) const&& {
545 if (*this) {
546 return std::invoke(std::forward<F>(f), std::move(**this));
547 }
548 return std::remove_cvref_t<std::invoke_result_t<F, const T>>{};
549 }
550
563 template <typename F>
564 constexpr auto transform(F&& f) & {
565 using U = std::remove_cv_t<std::invoke_result_t<F, T&>>;
566 if (*this) {
567 return Optional<U>{std::in_place, std::invoke(std::forward<F>(f), **this)};
568 }
569 return Optional<U>{};
570 }
571
584 template <typename F>
585 constexpr auto transform(F&& f) const& {
586 using U = std::remove_cv_t<std::invoke_result_t<F, const T&>>;
587 if (*this) {
588 return Optional<U>{std::in_place, std::invoke(std::forward<F>(f), **this)};
589 }
590 return Optional<U>{};
591 }
592
605 template <typename F>
606 constexpr auto transform(F&& f) && {
607 using U = std::remove_cv_t<std::invoke_result_t<F, T>>;
608 if (*this) {
609 return Optional<U>{std::in_place, std::invoke(std::forward<F>(f), std::move(**this))};
610 }
611 return Optional<U>{};
612 }
613
626 template <typename F>
627 constexpr auto transform(F&& f) const&& {
628 using U = std::remove_cv_t<std::invoke_result_t<F, const T>>;
629 if (*this) {
630 return Optional<U>{std::in_place, std::invoke(std::forward<F>(f), std::move(**this))};
631 }
632 return Optional<U>{};
633 }
634
644 template <typename F>
645 constexpr Optional or_else(F&& f) const& {
646 return (*this) ? *this : std::invoke(std::forward<F>(f));
647 }
648
658 template <typename F>
659 constexpr Optional or_else(F&& f) && {
660 return (*this) ? std::move(*this) : std::invoke(std::forward<F>(f));
661 }
662
671 constexpr void swap(Optional& other) noexcept(
672 std::is_nothrow_move_constructible_v<T> && std::is_nothrow_swappable_v<T>) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
673 variant.swap(other.variant);
674 }
675
685 friend constexpr void swap(Optional& a, Optional& b) noexcept(noexcept(a.swap(b))) { // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
686 a.swap(b);
687 }
688
693 constexpr void reset() noexcept {
694 variant.template emplace<0>();
695 }
696
705 template <typename... Args>
706 constexpr T& emplace(Args&&... args) requires(std::is_constructible_v<T, Args...>) {
707 return variant.template emplace<1>(std::forward<Args>(args)...);
708 }
709
720 template <typename U, typename... Args>
721 constexpr T& emplace(std::initializer_list<U> ilist, Args&&... args) requires(std::is_constructible_v<T, std::initializer_list<U>&, Args...>) {
722 return variant.template emplace<1>(ilist, std::forward<Args>(args)...);
723 }
724
725private:
726 Variant<Monostate, T> variant;
727};
728
741template <typename T, typename U>
742[[nodiscard]] constexpr bool operator==(const Optional<T>& a, const Optional<U>& b) requires equality_comparable<T, U> {
743 if (a && b) {
744 return *a == *b;
745 }
746 return static_cast<bool>(a) == static_cast<bool>(b);
747}
748
757template <typename T>
758[[nodiscard]] constexpr bool operator==(Nullopt value, const Optional<T>& opt) {
759 (void)value;
760 return !static_cast<bool>(opt);
761}
762
771template <typename T>
772[[nodiscard]] constexpr bool operator==(const Optional<T>& opt, Nullopt value) {
773 (void)value;
774 return !static_cast<bool>(opt);
775}
776
789template <typename T, typename U>
790[[nodiscard]] constexpr bool operator==(const Optional<T>& opt, const U& value) requires(!template_specialization_of<std::remove_cvref_t<U>, Optional> && equality_comparable<T, U>)
791{
792#ifdef _MSC_VER
793#pragma warning(push)
794#pragma warning(disable : 4389)
795#elif defined(__clang__)
796#pragma clang diagnostic push
797#pragma clang diagnostic ignored "-Wsign-compare"
798#elif defined(__GNUC__)
799#pragma GCC diagnostic push
800#pragma GCC diagnostic ignored "-Wsign-compare"
801#endif
802 return (opt) ? *opt == value : false;
803#ifdef _MSC_VER
804#pragma warning(pop)
805#elif defined(__clang__)
806#pragma clang diagnostic pop
807#elif defined(__GNUC__)
808#pragma GCC diagnostic pop
809#endif
810}
811
824template <typename T, typename U>
825[[nodiscard]] constexpr bool operator==(const U& value, const Optional<T>& opt) requires(!template_specialization_of<std::remove_cvref_t<U>, Optional> && equality_comparable<U, T>)
826{
827#ifdef __clang__
828#pragma clang diagnostic push
829#pragma clang diagnostic ignored "-Wsign-compare"
830#elif defined(__GNUC__)
831#pragma GCC diagnostic push
832#pragma GCC diagnostic ignored "-Wsign-compare"
833#endif
834 return (opt) ? *opt == value : false;
835#ifdef __clang__
836#pragma clang diagnostic pop
837#elif defined(__GNUC__)
838#pragma GCC diagnostic pop
839#endif
840}
841
855template <typename T, typename U>
856[[nodiscard]] constexpr std::compare_three_way_result_t<T, U> operator<=>(const Optional<T>& a, const Optional<U>& b) requires three_way_comparable<T, U> {
857 if (a && b) {
858 return *a <=> *b;
859 }
860 return static_cast<bool>(a) <=> static_cast<bool>(b);
861}
862
871template <typename T>
872[[nodiscard]] constexpr std::strong_ordering operator<=>(const Optional<T>& opt, Nullopt value) {
873 (void)value;
874 return static_cast<bool>(opt) <=> false;
875}
876
890template <typename T, typename U>
891[[nodiscard]] constexpr std::compare_three_way_result_t<T, U> operator<=>(const Optional<T>& opt, const U& value)
893 return (opt) ? *opt <=> value : std::strong_ordering::less;
894}
895
909template <typename T, typename U>
910[[nodiscard]] constexpr std::compare_three_way_result_t<U, T> operator<=>(const U& value, const Optional<T>& opt)
912 return (opt) ? value <=> *opt : std::strong_ordering::greater;
913}
914
915} // namespace grem
916
917template <typename T>
918struct std::hash<grem::Optional<T>> {
919 [[nodiscard]] std::size_t operator()(const grem::Optional<T>& opt) const {
920 if (opt) {
921 return hasher(*opt);
922 }
923 return static_cast<std::size_t>(0xFFFFFFFFFFFFF2FBull);
924 }
925
926private:
927 [[no_unique_address]] std::hash<std::remove_const_t<T>> hasher;
928};
929
930#endif
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
constexpr void swap(Optional &other) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_swappable_v< T >)
Swap this optional's value with that of another.
Definition Optional.hpp:671
constexpr ~Optional()=default
Destructor.
constexpr T && value() &&
Access the underlying value.
Definition Optional.hpp:422
constexpr T & emplace(std::initializer_list< U > ilist, Args &&... args)
Construct a new underlying value, with an initializer list as the first constructor argument,...
Definition Optional.hpp:721
constexpr T value_or(U &&defaultValue) const &
Get the underlying value, or a fallback value in case the optional is empty.
Definition Optional.hpp:453
constexpr Optional(std::in_place_t tag, Args &&... args)
Construct an optional with an active value in-place.
Definition Optional.hpp:84
constexpr Optional(Optional< U > &&other)
Implicit converting move constructor.
Definition Optional.hpp:178
constexpr Optional & operator=(U &&value)
Converting assignment.
Definition Optional.hpp:223
constexpr T & emplace(Args &&... args)
Construct a new underlying value, destroying the old value if present.
Definition Optional.hpp:706
constexpr T && operator*() &&noexcept
Access the underlying value without a safety check.
Definition Optional.hpp:363
constexpr Optional & operator=(Nullopt) noexcept
Destroy the underlying value if present and reset the optional to an empty state.
Definition Optional.hpp:208
constexpr Optional(const Optional &other)=default
Copy constructor.
constexpr Optional & operator=(Optional &&other) noexcept(std::is_nothrow_move_constructible_v< T > &&//NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor) std::is_nothrow_move_assignable_v< T >)=default
Move assignment.
constexpr bool has_value() const noexcept
Check if the optional contains a value.
Definition Optional.hpp:383
constexpr Optional(U &&value)
Implicit converting constructor.
Definition Optional.hpp:117
constexpr auto transform(F &&f) &&
Invoke a function with the underlying value, or return an empty optional in case the optional is empt...
Definition Optional.hpp:606
constexpr const T && value() const &&
Access the underlying value.
Definition Optional.hpp:436
constexpr auto and_then(F &&f) &
Invoke a function with the underlying value, or return a default-constructed result in case the optio...
Definition Optional.hpp:484
constexpr Optional(std::in_place_t tag, std::initializer_list< U > ilist, Args &&... args)
Construct an optional with an active value in-place, with an initializer list as the first constructo...
Definition Optional.hpp:103
constexpr void reset() noexcept
Destroy the underlying value if present and reset the optional to an empty state.
Definition Optional.hpp:693
constexpr auto transform(F &&f) const &&
Invoke a function with the underlying value, or return an empty optional in case the optional is empt...
Definition Optional.hpp:627
constexpr auto and_then(F &&f) const &
Invoke a function with the underlying value, or return a default-constructed result in case the optio...
Definition Optional.hpp:504
friend constexpr void swap(Optional &a, Optional &b) noexcept(noexcept(a.swap(b)))
Swap the values of two optionals.
Definition Optional.hpp:685
constexpr auto and_then(F &&f) &&
Invoke a function with the underlying value, or return a default-constructed result in case the optio...
Definition Optional.hpp:524
constexpr auto transform(F &&f) &
Invoke a function with the underlying value, or return an empty optional in case the optional is empt...
Definition Optional.hpp:564
constexpr T value_or(U &&defaultValue) &&
Get the underlying value, or a fallback value in case the optional is empty.
Definition Optional.hpp:467
constexpr const T * operator->() const noexcept
Access the underlying value by pointer without a safety check.
Definition Optional.hpp:308
constexpr Optional or_else(F &&f) const &
Get an optional containing the underlying value, or the result of a function in case the optional is ...
Definition Optional.hpp:645
constexpr T & operator*() &noexcept
Access the underlying value without a safety check.
Definition Optional.hpp:341
constexpr Optional(Nullopt value) noexcept
Construct an empty optional.
Definition Optional.hpp:70
constexpr Optional(U &&value)
Explicit converting constructor.
Definition Optional.hpp:132
constexpr Optional & operator=(const Optional< U > &other)
Converting copy assignment.
Definition Optional.hpp:263
constexpr const T & operator*() const &noexcept
Access the underlying value without a safety check.
Definition Optional.hpp:330
constexpr T * operator->() noexcept
Access the underlying value by pointer without a safety check.
Definition Optional.hpp:319
constexpr Optional(const Optional< U > &other)
Explicit converting copy constructor.
Definition Optional.hpp:164
constexpr Optional & operator=(const Optional &other)=default
Copy assignment.
constexpr auto transform(F &&f) const &
Invoke a function with the underlying value, or return an empty optional in case the optional is empt...
Definition Optional.hpp:585
constexpr const T && operator*() const &&noexcept
Access the underlying value without a safety check.
Definition Optional.hpp:352
constexpr Optional(Optional< U > &&other)
Explicit converting move constructor.
Definition Optional.hpp:192
constexpr Optional & operator=(Optional< U > &&other)
Converting move assignment.
Definition Optional.hpp:283
constexpr T & value() &
Access the underlying value.
Definition Optional.hpp:394
constexpr Optional(const Optional< U > &other)
Implicit converting copy constructor.
Definition Optional.hpp:150
constexpr Optional or_else(F &&f) &&
Get an optional containing the underlying value, or the result of a function in case the optional is ...
Definition Optional.hpp:659
constexpr Optional() noexcept
Construct an empty optional.
Definition Optional.hpp:63
constexpr const T & value() const &
Access the underlying value.
Definition Optional.hpp:408
constexpr auto and_then(F &&f) const &&
Invoke a function with the underlying value, or return a default-constructed result in case the optio...
Definition Optional.hpp:544
constexpr Optional(Optional &&other) noexcept(std::is_nothrow_move_constructible_v< T >)=default
Move constructor.
Tagged union value type that holds a value of one of the given types.
Definition Variant.hpp:293
Definition concepts.hpp:160
Definition concepts.hpp:41
Definition concepts.hpp:163
Definition aliases.hpp:9
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
constexpr Nullopt nullopt
Unit value representing an empty Optional.
Definition Optional.hpp:35
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
Exception type that is thrown on an attempt to erroneously access an empty Optional when using a safe...
Definition Optional.hpp:41
BadOptionalAccess() noexcept=default
const char * what() const noexcept override
Definition Optional.hpp:44
Unit type for representing an empty Optional.
Definition Optional.hpp:25
constexpr Nullopt(int)
Constructor ensuring that Nullopt can only be constructed explicitly.
Definition Optional.hpp:29
std::size_t operator()(const grem::Optional< T > &opt) const
Definition Optional.hpp:919