4.4.1
Freundlich's C++ toolkit
Loading...
Searching...
No Matches
Classes | Typedefs | Functions | Variables
fcppt.optional

Description

A class that makes values optional.

Motivation

The goal of an optional type is to make it explicit that a value might be missing. This includes operations that might return no result, for example looking for an element in a container, performing a conversion that can fail, a tree that might have no parent, and so on. Values that may be nothing are common in C++ but are not handled consistently. This includes special values like std::string::npos, nullptr or end iterators. Modelling optionals explicitly has several advantages. Consider the following example:

void optional_example_bad(fcppt::optional::object<unsigned> const _opt)
{
if (_opt.has_value())
{
std::cout << _opt.get_unsafe() << '\n';
}
else
{
std::cout << "No value\n";
}
}

First of all, the example makes it clear that the function can deal with an unsigned int or nothing at all. However, using optionals in this way makes them no safer than using pointers that may be null. In order to fix this, we must avoid calling get_unsafe manually. The most basic operation that does this for us is called maybe. The user provides two functions which act on the two different cases.

void optional_example_good(fcppt::optional::object<unsigned> const _opt)
{
_opt,
[] { std::cout << "No value\n"; },
[](unsigned const _val) { std::cout << _val << '\n'; });
}

Notice that by using maybe, the code checking if the optional has a value and the code dereferencing the optional are both gone.

Design

The key differences to std::optional include:

Optional References

Instead of implementing optional references directly, fcppt offers fcppt::optional::reference which is a typedef to fcppt::optional::object<fcppt::reference<T>>. The reason behind this is that code that acts on optionals might make use of references itself. For example it is not uncommon to pass the object held by an optional to functions like fcppt::optional::maybe_void by (const) reference, which can be used to change the object or to pass its address along.

using optional_int = fcppt::optional::object<int>;
void set_ref(optional_int _opt_ref)
{
fcppt::optional::maybe_void(_opt_ref, [](int &_ref) { _ref = 1; });
}

On the other hand, if an optional holds a reference it does not hold the actual object, so changing the object behind the reference or passing the reference along has different semantics.

using optional_int_ref = fcppt::optional::reference<int>;
void set_ref2(optional_int_ref const _opt_ref)
{
fcppt::optional::maybe_void(_opt_ref, [](fcppt::reference<int> const _ref) { _ref.get() = 1; });
}

Using fcppt::reference for optional references clearly distinguishes these two cases.

Common Operations

fcppt::optional::maybe is the most basic function that operates on optionals.

fcppt::optional::map lifts a function to a function on optionals:

optional_string const value2{fcppt::optional::map(
optional_uint{1U}, [](unsigned const _val) { return std::to_string(_val); })};
std::cout << (value == optional_string{"1"}) << '\n';

The more general fcppt::optional::bind returns the function's result directly:

using optional_uint = fcppt::optional::object<unsigned>;
using optional_string = fcppt::optional::object<std::string>;
optional_string const value{fcppt::optional::bind(optional_uint{1U}, [](unsigned const _val) {
return optional_string(std::to_string(_val));
})};
std::cout << (value == optional_string{"1"}) << '\n';

Another commonly used function is fcppt::optional::from:

using optional_uint = fcppt::optional::object<unsigned>;
unsigned const value{fcppt::optional::from(optional_uint(), [] { return 1U; })};
std::cout << (value == 1U) << '\n';

Header files

Header file Description
object_fwd.hpp Contains fcppt::optional::object optional's declaration.
object_decl.hpp Contains fcppt::optional::object optional's definition.
object_impl.hpp Contains the definition of fcppt::optional::object optional's member functions.
object.hpp Includes object_impl.hpp and comparison.hpp.
alternative.hpp Contains fcppt::optional::alternative .
apply.hpp Contains fcppt::optional::apply .
assign.hpp Contains fcppt::optional::assign .
bind.hpp Contains fcppt::optional::bind .
cat.hpp Contains fcppt::optional::cat .
combine.hpp Contains fcppt::optional::combine .
comparison.hpp Contains operator==, operator!= and operator< .
copy_value.hpp Contains fcppt::optional::copy_value .
deref.hpp Contains fcppt::optional::deref .
filter.hpp Contains fcppt::optional::filter .
from.hpp Contains fcppt::optional::from .
from_pointer.hpp Contains fcppt::optional::from_pointer .
join.hpp Contains fcppt::optional::join .
make.hpp Contains fcppt::optional::make .
map.hpp Contains fcppt::optional::map .
maybe.hpp Contains fcppt::optional::maybe .
maybe_multi.hpp Contains fcppt::optional::maybe_multi .
maybe_void.hpp Contains fcppt::optional::maybe_void .
maybe_void_multi.hpp Contains fcppt::optional::maybe_void_multi .
output.hpp Contains operator<< .
reference_fwd.hpp Contains the fcppt::optional::reference typedef.
reference.hpp Includes reference_fwd.hpp and object_impl.hpp.
sequence.hpp Contains fcppt::optional::sequence .
to_exception.hpp Contains fcppt::optional::to_exception .

Classes

struct  fcppt::optional::is_object< T >
 Checks if a given type is an fcppt::optional::object. More...
 
struct  fcppt::optional::nothing
 A polymorphic empty optional. More...
 
class  fcppt::optional::object< T >
 A class that makes values optional. More...
 

Typedefs

template<fcppt::optional::object_concept Optional>
using fcppt::optional::move_type = fcppt::move_if_rvalue_type< Optional, fcppt::optional::reference_type< Optional > >
 The moved inner type of an optional.
 
template<typename T >
using fcppt::optional::reference = fcppt::optional::object< fcppt::reference< T > >
 Optional of a reference.
 
template<fcppt::optional::object_concept Optional>
using fcppt::optional::reference_type = decltype(std::declval< Optional >().get_unsafe())
 The reference type of an optional.
 
template<fcppt::optional::object_concept Optional>
using fcppt::optional::value_type = fcppt::type_traits::value_type< std::remove_cvref_t< Optional > >
 The value type of an optional.
 

Functions

template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move<> Function>
requires std::is_same_v<std::remove_cvref_t<Optional>, std::invoke_result_t<Function>>
std::remove_cvref_t< Optional > fcppt::optional::alternative (Optional &&_optional1, Function const &_optional2)
 Returns the first optional if it is not nothing.
 
template<fcppt::optional::object_concept... Optionals, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optionals >... > Function>
fcppt::optional::object< std::invoke_result_t< Function, fcppt::optional::move_type< Optionals >... > > fcppt::optional::apply (Function const &_function, Optionals &&..._optionals)
 Applies a function to multiple optionals if each of them contains a value.
 
template<typename Element , typename Arg >
requires std::is_same_v<Element, std::remove_cv_t<Arg>>
Element & fcppt::optional::assign (fcppt::optional::object< Element > &_optional, Arg &&_arg)
 Assigns an optional and returns a reference to the optional's contents.
 
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optional > > Function>
requires fcppt::optional::is_object_v< std::invoke_result_t<Function, fcppt::optional::move_type<Optional>>>
std::invoke_result_t< Function, fcppt::optional::move_type< Optional > > fcppt::optional::bind (Optional &&_source, Function const &_function)
 Converts an optional of one type to another.
 
template<typename TargetContainer , typename Source >
TargetContainer fcppt::optional::cat (Source &&_source)
 Removes empty optionals from a range.
 
template<fcppt::optional::object_concept Optional1, fcppt::optional::object_concept Optional2, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optional1 >, fcppt::optional::move_type< Optional2 > > Function>
requires std:: is_same_v<fcppt::optional::value_type<Optional1>, fcppt::optional::value_type<Optional2>> && std::is_same_v< fcppt::optional::value_type<Optional1>, std::invoke_result_t< Function, fcppt::optional::move_type<Optional1>, fcppt::optional::move_type<Optional2>>>
fcppt::optional::object< fcppt::optional::value_type< Optional1 > > fcppt::optional::combine (Optional1 &&_optional1, Optional2 &&_optional2, Function const &_function)
 Combines two optionals.
 
template<typename T >
bool fcppt::optional::operator== (fcppt::optional::object< T > const &_a, fcppt::optional::object< T > const &_b)
 Compares two optionals for equality.
 
template<typename T >
bool fcppt::optional::operator!= (fcppt::optional::object< T > const &_a, fcppt::optional::object< T > const &_b)
 Compares two optionals for inequality.
 
template<typename T >
bool fcppt::optional::operator< (fcppt::optional::object< T > const &_a, fcppt::optional::object< T > const &_b)
 Compares two optionals lexicographically.
 
template<typename Type >
fcppt::optional::object< std::remove_cv_t< Type > > fcppt::optional::copy_value (fcppt::optional::reference< Type > const &_opt)
 Copies the value of an optional reference.
 
template<typename Element >
fcppt::optional::object< fcppt::reference< std::remove_reference_t< decltype(*std::declval< Element >())> > fcppt::optional::deref (fcppt::optional::object< Element > const &_optional)
 Dereferences the contents of an optional.
 
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move< fcppt::optional::value_type< Optional > > Function>
requires std::is_same_v<bool, std::invoke_result_t<Function, fcppt::optional::value_type<Optional>>>
std::remove_cvref_t< Optional > fcppt::optional::filter (Optional &&_source, Function const &_function)
 Filters an optional.
 
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move Default>
requires std::is_same_v<std::invoke_result_t<Default>, fcppt::optional::value_type<Optional>>
std::invoke_result_t< Default > fcppt::optional::from (Optional &&_optional, Default const _default)
 Returns the value contained in an optional or a default value.
 
template<typename T >
fcppt::optional::reference< T > fcppt::optional::from_pointer (T *const _pointer)
 Creates an optional reference from a pointer.
 
template<typename Element , fcppt::concepts::invocable_move<> Function>
requires std::is_same_v<std::invoke_result_t<Function>, Element>
Element & fcppt::optional::get_or_assign (fcppt::optional::object< Element > &_optional, Function const &_function)
 Assigns an optional if it is empty. Returns a reference to the optional's contents.
 
template<fcppt::optional::object_concept Optional>
requires fcppt::optional::is_object_v<fcppt::optional::value_type<Optional>>
fcppt::optional::value_type< Optional > fcppt::optional::join (Optional &&_source)
 Removes one layer of optionals.
 
template<typename Type >
fcppt::optional::object< std::remove_cvref_t< Type > > fcppt::optional::make (Type &&_value)
 Wraps a value into an optional.
 
template<fcppt::concepts::invocable_move Function>
fcppt::optional::object< std::invoke_result_t< Function > > fcppt::optional::make_if (bool const _is_set, Function const &_function)
 Creates an optional depending on a boolean.
 
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optional > > Function>
fcppt::optional::object< std::invoke_result_t< Function, fcppt::optional::move_type< Optional > > > fcppt::optional::map (Optional &&_source, Function const &_function)
 Maps over an optional using a function.
 
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable Default, fcppt::concepts::invocable< fcppt::optional::move_type< Optional > > Transform>
requires std::is_same_v< std::invoke_result_t<Default>, std::invoke_result_t<Transform, fcppt::optional::move_type<Optional>>>
std::invoke_result_t< Default > fcppt::optional::maybe (Optional &&_optional, Default const &_default, Transform const &_transform)
 Transforms an optional value or returns a default value.
 
template<fcppt::optional::object_concept... Optionals, fcppt::concepts::invocable Default, fcppt::concepts::invocable< fcppt::optional::move_type< Optionals >... > Transform>
requires std::is_same_v< std::invoke_result_t<Default>, std::invoke_result_t<Transform, fcppt::optional::move_type<Optionals>...>>
std::invoke_result_t< Default > fcppt::optional::maybe_multi (Default const _default, Transform const _transform, Optionals &&..._optionals)
 Transforms multiple optional values or returns a default value.
 
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable< fcppt::optional::move_type< Optional > > Transform>
void fcppt::optional::maybe_void (Optional &&_optional, Transform const &_transform)
 Transforms an optional value or does nothing.
 
template<fcppt::optional::object_concept... Optionals, fcppt::concepts::invocable< fcppt::optional::move_type< Optionals >... > Transform>
void fcppt::optional::maybe_void_multi (Transform const _transform, Optionals &&..._optionals)
 Transforms optional values or does nothing.
 
template<typename Type , typename Ch , typename Traits >
std::basic_ostream< Ch, Traits > & fcppt::optional::operator<< (std::basic_ostream< Ch, Traits > &_stream, fcppt::optional::object< Type > const &_opt_value)
 Outputs an optional to a basic_ostream.
 
template<typename ResultContainer , typename Source >
fcppt::optional::object< ResultContainer > fcppt::optional::sequence (Source &&_source)
 Turns a container of optionals into an optional container.
 
template<typename Container , fcppt::optional::object_concept Optional>
requires std::is_same_v<fcppt::type_traits::value_type<Container>, fcppt::optional::value_type<Optional>>
Container fcppt::optional::to_container (Optional &&_source)
 Puts the value of an optional into a container or returns an empty container.
 
template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move MakeException>
fcppt::optional::move_type< Optional > fcppt::optional::to_exception (Optional &&_optional, MakeException const _make_exception)
 Returns the value contained in an optional or throws an exception.
 
template<typename T >
T * fcppt::optional::to_pointer (fcppt::optional::reference< T > const _optional)
 Converts an optional reference to a pointer.
 

Variables

template<typename T >
constexpr bool fcppt::optional::is_object_v = fcppt::optional::is_object<T>::value
 Checks if a given type is an fcppt::optional::object.
 

Typedef Documentation

◆ move_type

The moved inner type of an optional.

◆ reference

template<typename T >
using fcppt::optional::reference = typedef fcppt::optional::object<fcppt::reference<T> >

Optional of a reference.

◆ reference_type

template<fcppt::optional::object_concept Optional>
using fcppt::optional::reference_type = typedef decltype(std::declval<Optional>().get_unsafe())

The reference type of an optional.

◆ value_type

template<fcppt::optional::object_concept Optional>
using fcppt::optional::value_type = typedef fcppt::type_traits::value_type<std::remove_cvref_t<Optional> >

The value type of an optional.

Function Documentation

◆ alternative()

template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move<> Function>
requires std::is_same_v<std::remove_cvref_t<Optional>, std::invoke_result_t<Function>>
std::remove_cvref_t< Optional > fcppt::optional::alternative ( Optional &&  _optional1,
Function const &  _optional2 
)

Returns the first optional if it is not nothing.

If _optional1 is not nothing, the result is _optional1, otherwise the result of _optional2 is returned.

◆ apply()

template<fcppt::optional::object_concept... Optionals, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optionals >... > Function>
fcppt::optional::object< std::invoke_result_t< Function, fcppt::optional::move_type< Optionals >... > > fcppt::optional::apply ( Function const &  _function,
Optionals &&...  _optionals 
)
inline

Applies a function to multiple optionals if each of them contains a value.

If _optionals, o_1,...,o_n are set to x_1,...,x_n, then optional::object{_function(x_1,...,x_n)} is returned. Otherwise, the empty optional is returned.

◆ assign()

template<typename Element , typename Arg >
requires std::is_same_v<Element, std::remove_cv_t<Arg>>
Element & fcppt::optional::assign ( fcppt::optional::object< Element > &  _optional,
Arg &&  _arg 
)
inline

Assigns an optional and returns a reference to the optional's contents.

Assigns _arg to _optional and returns a reference to _arg.

◆ bind()

template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optional > > Function>
requires fcppt::optional::is_object_v< std::invoke_result_t<Function, fcppt::optional::move_type<Optional>>>
std::invoke_result_t< Function, fcppt::optional::move_type< Optional > > fcppt::optional::bind ( Optional &&  _source,
Function const &  _function 
)
inline

Converts an optional of one type to another.

If _source is set to x, then _function(x) is returned. Otherwise, the empty optional is returned.

◆ cat()

template<typename TargetContainer , typename Source >
TargetContainer fcppt::optional::cat ( Source &&  _source)

Removes empty optionals from a range.

For every element e in _source, if e is set to x, then x is inserted into the target container.

Template Parameters
SourceMust be a range of optionals.
TargetContainerMust be a container whose value type matches that of the optionals from Source

TODO(concepts)

◆ combine()

template<fcppt::optional::object_concept Optional1, fcppt::optional::object_concept Optional2, fcppt::concepts::invocable_move< fcppt::optional::move_type< Optional1 >, fcppt::optional::move_type< Optional2 > > Function>
requires std:: is_same_v<fcppt::optional::value_type<Optional1>, fcppt::optional::value_type<Optional2>> && std::is_same_v< fcppt::optional::value_type<Optional1>, std::invoke_result_t< Function, fcppt::optional::move_type<Optional1>, fcppt::optional::move_type<Optional2>>>
fcppt::optional::object< fcppt::optional::value_type< Optional1 > > fcppt::optional::combine ( Optional1 &&  _optional1,
Optional2 &&  _optional2,
Function const &  _function 
)

Combines two optionals.

If _optional1 is set to x1 and _optional2 is set to x2, then the result is Optional(_function(x1, x2)). Otherwise, if at least one optional is set, that optional is returned.

◆ copy_value()

template<typename Type >
fcppt::optional::object< std::remove_cv_t< Type > > fcppt::optional::copy_value ( fcppt::optional::reference< Type > const &  _opt)

Copies the value of an optional reference.

◆ deref()

template<typename Element >
fcppt::optional::object< fcppt::reference< std::remove_reference_t< decltype(*std::declval< Element >())> > fcppt::optional::deref ( fcppt::optional::object< Element > const &  _optional)
inline

Dereferences the contents of an optional.

If the optional is set to x, make_(c)ref(*x) is returned. For example, this is useful as a shortcut for optionals containing iterators or unique pointers.

TODO(concepts)

◆ filter()

template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move< fcppt::optional::value_type< Optional > > Function>
requires std::is_same_v<bool, std::invoke_result_t<Function, fcppt::optional::value_type<Optional>>>
std::remove_cvref_t< Optional > fcppt::optional::filter ( Optional &&  _source,
Function const &  _function 
)

Filters an optional.

If _source is set to x and _function(x) returns true, _source is returned. Otherwise, the empty optional is returned.

◆ from()

template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable_move Default>
requires std::is_same_v<std::invoke_result_t<Default>, fcppt::optional::value_type<Optional>>
std::invoke_result_t< Default > fcppt::optional::from ( Optional &&  _optional,
Default const  _default 
)

Returns the value contained in an optional or a default value.

If _optional is set to x, then x is returned. Otherwise, the result of _default is returned.

◆ from_pointer()

template<typename T >
fcppt::optional::reference< T > fcppt::optional::from_pointer ( T *const  _pointer)

Creates an optional reference from a pointer.

Creates an optional reference from _pointer. If _pointer is the null pointer, the result will be empty. Otherwise, the result will contain a reference to *_pointer.

◆ get_or_assign()

template<typename Element , fcppt::concepts::invocable_move<> Function>
requires std::is_same_v<std::invoke_result_t<Function>, Element>
Element & fcppt::optional::get_or_assign ( fcppt::optional::object< Element > &  _optional,
Function const &  _function 
)
inline

Assigns an optional if it is empty. Returns a reference to the optional's contents.

Assigns the result of _function to _optional if _optional is empty and returns a reference to the contents of _optional.

◆ join()

fcppt::optional::value_type< Optional > fcppt::optional::join ( Optional &&  _source)
inline

Removes one layer of optionals.

If _source is set to optional{x}, then optional{x} is returned. Otherwise, the empty optional is returned.

◆ make()

template<typename Type >
fcppt::optional::object< std::remove_cvref_t< Type > > fcppt::optional::make ( Type &&  _value)
inline

Wraps a value into an optional.

◆ make_if()

template<fcppt::concepts::invocable_move Function>
fcppt::optional::object< std::invoke_result_t< Function > > fcppt::optional::make_if ( bool const  _is_set,
Function const &  _function 
)
inline

Creates an optional depending on a boolean.

If _is_set is true, then _function() is returned as an optional. Otherwise, the empty optional is returned.

◆ map()

fcppt::optional::object< std::invoke_result_t< Function, fcppt::optional::move_type< Optional > > > fcppt::optional::map ( Optional &&  _source,
Function const &  _function 
)
inline

Maps over an optional using a function.

If _source is set to x, then fcppt::optional::make(_function(x)) is returned. Otherwise, the empty optional is returned.

◆ maybe()

template<fcppt::optional::object_concept Optional, fcppt::concepts::invocable Default, fcppt::concepts::invocable< fcppt::optional::move_type< Optional > > Transform>
requires std::is_same_v< std::invoke_result_t<Default>, std::invoke_result_t<Transform, fcppt::optional::move_type<Optional>>>
std::invoke_result_t< Default > fcppt::optional::maybe ( Optional &&  _optional,
Default const &  _default,
Transform const &  _transform 
)

Transforms an optional value or returns a default value.

For the lack of a better name, this function is called maybe like in Haskell. If _optional is set to x, then _transform(x) is returned. Otherwise, the result of _default is returned.

◆ maybe_multi()

template<fcppt::optional::object_concept... Optionals, fcppt::concepts::invocable Default, fcppt::concepts::invocable< fcppt::optional::move_type< Optionals >... > Transform>
requires std::is_same_v< std::invoke_result_t<Default>, std::invoke_result_t<Transform, fcppt::optional::move_type<Optionals>...>>
std::invoke_result_t< Default > fcppt::optional::maybe_multi ( Default const  _default,
Transform const  _transform,
Optionals &&...  _optionals 
)

Transforms multiple optional values or returns a default value.

A multi version of fcppt::optional::maybe. If _optionals are set to x_1, ..., x_n, then _transform(x_1, ..., x_n) is returned. Otherwise, the result of _default is returned.

◆ maybe_void()

void fcppt::optional::maybe_void ( Optional &&  _optional,
Transform const &  _transform 
)
inline

Transforms an optional value or does nothing.

If _optional is set to x, then _transform(x) is called.

◆ maybe_void_multi()

template<fcppt::optional::object_concept... Optionals, fcppt::concepts::invocable< fcppt::optional::move_type< Optionals >... > Transform>
void fcppt::optional::maybe_void_multi ( Transform const  _transform,
Optionals &&...  _optionals 
)
inline

Transforms optional values or does nothing.

A multi version of fcppt::optional::maybe_void. If _optionals are set to x_1, ..., x_n, then _transform(x_1, ..., x_n) is called.

◆ operator!=()

template<typename T >
bool fcppt::optional::operator!= ( fcppt::optional::object< T > const &  _a,
fcppt::optional::object< T > const &  _b 
)

Compares two optionals for inequality.

Compares _a and _b for inequality. Equal to !(_a == _b). This function requires T to be equality comparable.

TODO(concepts)

◆ operator<()

template<typename T >
bool fcppt::optional::operator< ( fcppt::optional::object< T > const &  _a,
fcppt::optional::object< T > const &  _b 
)

Compares two optionals lexicographically.

Compares _a and _b lexicographically. If one or both of them are empty, returns _a.has_value() < _b.has_value(), otherwise returns _a.get_unsafe() < _b.get_unsafe().

TODO(concepts)

◆ operator<<()

template<typename Type , typename Ch , typename Traits >
std::basic_ostream< Ch, Traits > & fcppt::optional::operator<< ( std::basic_ostream< Ch, Traits > &  _stream,
fcppt::optional::object< Type > const &  _opt_value 
)

Outputs an optional to a basic_ostream.

TODO(concepts)

◆ operator==()

template<typename T >
bool fcppt::optional::operator== ( fcppt::optional::object< T > const &  _a,
fcppt::optional::object< T > const &  _b 
)

Compares two optionals for equality.

Compares _a and _b for equality. Two optionals are equal if they are either both empty or if they are both not empty and their elements compare equal. This function requires T to be equality comparable.

TODO(concepts)

◆ sequence()

template<typename ResultContainer , typename Source >
fcppt::optional::object< ResultContainer > fcppt::optional::sequence ( Source &&  _source)

Turns a container of optionals into an optional container.

Let _source be a container [o_1,...o_n] of type fcppt::optional::object<T>. If there is an i such that o_i is nothing, then nothing is returned. Otherwise, all optionals have a value, [v_1,...,v_n], and fcppt::optional::object<ResultContainer>{v_1,...,v_n} is returned.

Template Parameters
ResultContainerMust be a container of type T
SourceMust be an optional type

TODO(concepts)

◆ to_container()

template<typename Container , fcppt::optional::object_concept Optional>
requires std::is_same_v<fcppt::type_traits::value_type<Container>, fcppt::optional::value_type<Optional>>
Container fcppt::optional::to_container ( Optional &&  _source)

Puts the value of an optional into a container or returns an empty container.

If _source holds x, then Container{x} is returned. Otherwise the empty container is returned.

Container and Optional must have the same value types.

TODO(concepts)

◆ to_exception()

fcppt::optional::move_type< Optional > fcppt::optional::to_exception ( Optional &&  _optional,
MakeException const  _make_exception 
)
inline

Returns the value contained in an optional or throws an exception.

If _optional is set to x, then x is returned. Otherwise, the result of _make_exception is thrown as an exception.

◆ to_pointer()

template<typename T >
T * fcppt::optional::to_pointer ( fcppt::optional::reference< T > const  _optional)
inline

Converts an optional reference to a pointer.

If _optional is empty, returns nullptr. Otherwise, returns the address of the referenced object of _optional.

Variable Documentation

◆ is_object_v

template<typename T >
constexpr bool fcppt::optional::is_object_v = fcppt::optional::is_object<T>::value
inlineconstexpr

Checks if a given type is an fcppt::optional::object.