Inherits MaybeBase< T >.
|
constexpr | maybe () noexcept |
| Constructs maybe in a disengaged state.
|
|
constexpr | maybe (nothing_t) noexcept |
| Constructs maybe in a disengaged state.
|
|
constexpr | maybe (const T &value) |
| Constructs maybe in an engaged state containing the value.
|
|
constexpr | maybe (T &&value) |
| Constructs maybe in an engaged state containing the value.
|
|
template<typename... Args> |
constexpr | maybe (in_place_t, Args &&...args) |
| Constructs maybe in place. More...
|
|
| maybe (const maybe &rhs) |
| Copy constructor. More...
|
|
| maybe (maybe &&rhs) noexcept(std::is_nothrow_move_constructible< T >()) |
| Move constructor. More...
|
|
maybe & | operator= (nothing_t) noexcept |
| Assignment with nothing . More...
|
|
maybe & | operator= (const maybe &rhs) |
| Copy assignment. Behaves similar to the copy constructor.
|
|
maybe & | operator= (maybe &&rhs) noexcept(std::is_nothrow_move_assignable< T >()&&std::is_nothrow_move_constructible< T >()) |
| Move assignment. Behaves similar to the move constructor.
|
|
template<typename U , meta::EnableIf< std::is_constructible< T, meta::Decay< U >>, std::is_assignable< T, meta::Decay< U >>> = meta::_> |
maybe & | operator= (U &&value) |
| Assigns maybe in an engaged state with the value given. More...
|
|
template<typename... Args> |
void | emplace (Args &&...args) |
| Constructs the contained value in place. More...
|
|
constexpr | operator bool () const noexcept |
| Checks if the current state is engaged. More...
|
|
template<typename U > |
constexpr T | value_or (U &&value) const |
| Accesses the contained value or a reasonable default. More...
|
|
|
constexpr const T * | operator-> () const |
| Returns a pointer to the contained value. More...
|
|
T * | operator-> () |
|
|
constexpr const T & | operator* () const |
| Returns a reference to the contained value. More...
|
|
T & | operator* () |
|
|
constexpr const T & | value () const |
| Accesses the contained value. More...
|
|
T & | value () |
|
|
(Note that these are not member functions.)
|
template<typename T > |
constexpr maybe< meta::Decay< T > > | just (T &&t) |
| Helper function to create an engaged maybe object. More...
|
|
|
Compares the contained value of both maybe<T> s.
If both maybe<T> are disengaged, then the comparison returns true . If only one maybe<T> is disengaged, then the comparison returns false . Otherwise it uses the internal comparison operator on both contained values.
- Parameters
-
lhs | Left hand side of the comparison |
rhs | Right hand side of the comparison |
|
template<typename T > |
constexpr bool | operator== (const maybe< T > &lhs, const maybe< T > &rhs) |
| Checks if two maybe<T> are equal.
|
|
template<typename T > |
constexpr bool | operator!= (const maybe< T > &lhs, const maybe< T > &rhs) |
| Checks if two maybe<T> are not equal.
|
|
template<typename T > |
constexpr bool | operator< (const maybe< T > &lhs, const maybe< T > &rhs) |
| Checks if one maybe<T> is less than another maybe<T> .
|
|
template<typename T > |
constexpr bool | operator> (const maybe< T > &lhs, const maybe< T > &rhs) |
| Checks if one maybe<T> is greater than another maybe<T> .
|
|
template<typename T > |
constexpr bool | operator<= (const maybe< T > &lhs, const maybe< T > &rhs) |
| Checks if one maybe<T> is less than or equal to another maybe<T> .
|
|
template<typename T > |
constexpr bool | operator>= (const maybe< T > &lhs, const maybe< T > &rhs) |
| Checks if one maybe<T> is greater than or equal to another maybe<T> .
|
|
|
Compares maybe<T> with nothing .
For completeness sakes, you can compare nothing with maybe<T> . nothing will always evaluate to false . So all comparisons are based on truth values with maybe converted into a boolean true or false depending on the engaged state it is at. For example:
my_maybe == utility::nothing;
Where bool(my_maybe) is given through operator bool()
Although only one comparison is shown, all comparison operators are overloaded for both directions.
|
template<typename T > |
constexpr bool | operator== (const maybe< T > &lhs, nothing_t) noexcept |
| Compares with nothing
|
|
|
Compares the contained value with another value.
Although both directions are not shown, both directions of operator overloads are provided. Comparison is done through the operator overloaded, i.e. the operator== overload would compare the contained value and the other value with their operator== .
|
template<typename T > |
constexpr bool | operator== (const maybe< T > &lhs, const T &value) |
| If the current state is disengaged, returns false. Otherwise compares.
|
|
template<typename T > |
constexpr bool | operator!= (const maybe< T > &lhs, const T &value) |
| If the current state is disengaged, returns true. Otherwise compares.
|
|
template<typename T > |
constexpr bool | operator< (const maybe< T > &lhs, const T &value) |
| If the current state is disengaged, returns true. Otherwise compares.
|
|
template<typename T > |
constexpr bool | operator> (const maybe< T > &lhs, const T &value) |
| If the current state is disengaged, returns false. Otherwise compares.
|
|
template<typename T > |
constexpr bool | operator>= (const maybe< T > &lhs, const T &value) |
| If the current state is disengaged, returns false. Otherwise compares.
|
|
template<typename T > |
constexpr bool | operator<= (const maybe< T > &lhs, const T &value) |
| If the current state is disengaged, returns true. Otherwise compares.
|
|
template<typename T>
class gears::utility::maybe< T >
Implements an optional data type, similar to Haskell's Maybe
monad and Boost's boost::optional
. It manages a value that might not be there, similar to a pointer being possibly null. A lot of effort to make maybe<T>
be a possible constant expression was made. As a result of this, you can make maybe
be a constexpr
variable if needed.
The type to manage must not be nothing_t
, in_place_t
, a reference, or another maybe
. This might change in the future, but for now this is the restriction in place.
maybe
has two states – disengaged and engaged states. A disengaged state is when maybe
does not contain a value, i.e. it contains nothing
. An engaged state is when maybe
does contain a value to manage.
Example:
#include <gears/utility.hpp>
#include <iostream>
namespace util = gears::utility;
util::maybe<int> special(int x) {
return x > 10 ? util::just(x * 10) : util::nothing;
}
int main() {
auto x = special(8);
auto y = special(25);
if(x) {
std::cout << "x has a value\n";
}
if(y) {
std::cout << "y has a value\n";
}
std::cout << x.value_or(20) << '\n' << y.value_or(10) << '\n';
}
Output:
y has a value
20
250
- Template Parameters
-
T | Underlying type to manage. |
Definition at line 53 of file traits.hpp.