|
dot-xx
A personal collection of small C++ modules
|
An std::optional replacement with heap-allocated storage. More...
Public Member Functions | |
| constexpr | Option () noexcept |
| Default constructor - initialize an empty optional. | |
| constexpr | Option (std::nullopt_t) noexcept |
| Construct from std::nullopt - same as default. | |
| constexpr | Option (const Option &other) |
| Copy the optional value if the underlying type is copyable. | |
| constexpr | Option (Option &&other) noexcept |
| Transfer ownership over the value from another optional. | |
|
template<typename U> requires constructible_from_container<T, const U&, Option<U>> | |
| constexpr | explicit (!std::convertible_to< const U &, T >) Option(const Option< U > &other) |
| Construct from a different optional type. | |
|
template<typename U> requires constructible_from_container<T, U, Option<U>> | |
| constexpr | explicit (!std::convertible_to< U, T >) Option(Option< U > &&other) |
| Move from a different optional type. | |
|
template<typename... Args> requires std::constructible_from<T, Args...> | |
| constexpr | Option (std::in_place_t, Args &&... args) |
| Construct in-place. | |
|
template<typename U, typename... Args> requires std::constructible_from<T, std::initializer_list<U>&, Args...> | |
| constexpr | Option (std::in_place_t, std::initializer_list< U > i_list, Args &&... args) |
| Construct in-place with initializer list. | |
|
template<typename U = std::remove_cv_t<T>> requires requires { requires std::constructible_from<T, U>; requires !std::same_as<std::remove_cvref_t<U>, std::in_place_t>; requires !std::same_as<std::remove_cvref_t<U>, Option<T>>; requires !( std::same_as<std::remove_cv_t<T>, bool> && is_a<std::remove_cvref_t<U>, Option> ); } | |
| constexpr | explicit (!std::convertible_to< U, T >) Option(U &&value) |
| Construct with conversion. | |
| constexpr Option & | operator= (std::nullopt_t) |
| Reset the optional. | |
| constexpr Option & | operator= (const Option &other) |
| Copy from another optional. | |
| constexpr Option & | operator= (Option &&other) |
| Move in from another optional. | |
|
template<typename U> requires assignable_from_container<T, Option<U>> && std::constructible_from<T, const U&> && std::is_assignable_v<T&, const U&> | |
| constexpr Option & | operator= (const Option< U > &other) |
| Convert from another optional type. | |
|
template<typename U> requires assignable_from_container<T, Option<U>> && std::constructible_from<T, U> && std::is_assignable_v<T&, U> | |
| constexpr Option & | operator= (Option< U > &&other) |
| Move in from another optional type. | |
|
template<typename U = std::remove_cv_t<T>> requires requires { requires !std::same_as<std::remove_cvref_t<U>, Option<T>>; requires std::constructible_from<T, U>; requires std::is_assignable_v<T&, U>; requires (!std::is_scalar_v<T> || !std::same_as<std::decay_t<U>, T>); } | |
| constexpr Option & | operator= (U &&u) |
| Assign or construct the contained value from argument. | |
| constexpr | ~Option () |
| Destroys the contained value if any is allocated. | |
| constexpr | operator bool () const noexcept |
| Convert to bool. | |
| constexpr bool | has_value () const noexcept |
| Check if the optional contains a value. | |
| constexpr decltype(auto) | operator* (this auto &&self) |
| Access the underlying value without safety meeasures. | |
| constexpr auto * | operator-> (this auto &&self) |
| Access the contained value. | |
| constexpr decltype(auto) | value (this auto &&self) |
| Access the contained value in a safer way. | |
| template<typename U = std::remove_cv_t<T>> | |
| constexpr T | value_or (this auto &&self, U &&u) |
| Returns the contained value if exists, or a provided default value. | |
| constexpr void | swap (Option &other) noexcept |
| Swap the two optionals. | |
| constexpr void | reset () |
| Resets the state, destroys the contained value. | |
| constexpr T & | emplace (auto &&... args) |
Initialize the optional by constructing from args. | |
| template<typename U, typename... Args> | |
| constexpr T & | emplace (std::initializer_list< U > i_list, Args &&... args) |
Initialize the optional by constructing from i_list and args. | |
| template<typename Self, typename F> | |
| constexpr decltype(auto) | and_then (this Self &&self, F &&f) |
If the optional cotains a value, invokes f with the contained value as an argument and returns the result of that invokation, otherwise returns an empty optional. | |
| template<typename Self, typename F> | |
| constexpr decltype(auto) | transform (this Self &&self, F &&f) |
If the optional contains a value, returns an optional that contains the result of applying f to it, otherwise returns an empty optional. | |
| template<typename Self, typename F> | |
| constexpr std::remove_cvref_t< Self > | or_else (this Self &&self, F &&f) |
Returns a copy of the optional if it contains a value, otherwise returns the result of f. | |
An std::optional replacement with heap-allocated storage.
Practically useless and inferior alternative to std::optional except for the cases where a type needs to contain an optional member of the same type and refactoring this is not an option (e.g. when using reflection to read web APIs)
|
inlineconstexprnoexcept |
Transfer ownership over the value from another optional.
Resets the other optional to empty
|
inlinenodiscardconstexpr |
Access the contained value in a safer way.
| `std::bad_optional_access` | if the optional contains no value |