optional#
Fully qualified name: carb::cpp::optional
Defined in carb/cpp/Optional.h
-
template<typename T>
class optional : private detail::SelectHierarchy_t<detail::OptionalConstructor<T>, T># Template class to manage an optional contained value, i.e.
a value that may or may not be present.
A common use case for
optionalis the return value of a function that may fail. As opposed to other approaches such asstd::pair<T, bool>,optionalhandles expensive-to-construct objects well and is more readable, as the intent is expressed explicitly.Any instance of
optionalat any given point in time either contains a value or does not contain a value.If an
optionalcontains a value, the value is guaranteed to be nested within theoptionalobject. Thus, anoptionalobject models an object, not a pointer, even though operator* and operator-> are defined.When an object of type
optional<T>is contextually convertible to bool, the conversion returnstrueif theoptionalcontains a value andfalseif it does not contain a value.The
optionalobject contains a value in the following conditions:The object is initialized with/assigned from a value of type
Tor anotheroptionalthat contains a value.
The
optionalobject does not contain a value in the following conditions:The object is default-initialized.
The object is initialized with/assigned from a value of type nullopt_t or an
optionalobject that does not contain a value.The member function reset() is called.
The
optionalobject is a view that contains either one element if it contains a value, or otherwise zero elements if it does not contain a value. The lifetime of the contained element is bound to the object.There are no optional references, functions, arrays, or (possibly cv-qualified)
void; a program is ill-formed if it instantiates anoptionalwith such a type. In addition, a program is ill-formed if it instantiates anoptionalwith the (possibly cv-qualified) tag types nullopt_t orstd::in_place_t.Attempting to dereference an empty
optionalobject is considered an error and will throw an exception (or terminate if exceptions are not enabled).This implementation is intended to be a drop-in replacement for
std::optional(introduced in C++17) and existed when Carbonite’s minimum support was C++14. Since Carbonite requires C++17 or greater,std::optionalmay be used instead, however, this implementation adds additional features that are not available instd::optionaluntil even later versions of C++.Public Types
-
using iterator = omni::detail::PointerIterator<value_type*, optional<value_type>>#
Iterator type alias. (Introduced C++26)
-
using const_iterator = omni::detail::PointerIterator<const value_type*, optional<value_type>>#
Const Iterator type alias. (Introduced C++26)
Public Functions
-
inline constexpr optional() noexcept#
Constructor: constructs an empty optional object. The value in this object cannot be dereferenced.
-
inline constexpr optional(nullopt_t) noexcept#
Constructor: constructs an empty optional object. The value in this object cannot be dereferenced. This constructor variant allows for
nulloptto be assigned to this object as a way of resetting or emptying it.
-
inline optional(const optional &other)#
Copy constructor: constructs a new optional value by copying the state from another optional object.
- Parameters:
other – [in] The other optional object to copy the value or lack of value from.
- inline optional(
- optional &&other,
Move constructor: constructs a new optional value by moving the state from another optional object.
- Parameters:
other – [in] The other optional object to move the value or lack of value from. The value contained in
othermust be move constructible to the data type expected in this object. Theotherobject will be left in a valid but undefined state upon return.
-
inline optional &operator=(const optional &other)#
Copy-assignment operator.
- Parameters:
other – [in] The other optional object to copy the value or lack of value from.
- Returns:
A reference to this object.
- inline optional &operator=(
- optional &&other,
Move-assignment operator.
- Parameters:
other – [in] The other optional object to move the value or lack of value from. The value contained in
othermust be move constructible to the data type expected in this object. Theotherobject will be left in a valid but undefined state upon return.- Returns:
A reference to this object.
-
template<typename U, std::enable_if_t<AllowUnwrapping_v<U> && std::is_constructible_v<T, const U&> && std::is_convertible_v<const U&, T>, int> = 0>
inline optional(
)# Copy constructor: copies the value or lack of value from another optional object.
- Parameters:
other – [in] The other optional object to move the value or lack of value from. The value contained in
othermust be copy constructible to the data type expected in this object. Theotherobject will be left unmodified.
-
template<typename U, std::enable_if_t<AllowUnwrapping_v<U> && std::is_constructible_v<T, U> && std::is_convertible_v<U, T>, int> = 0>
inline optional(
)# Move constructor: moves the state from another optional object into this one.
- Parameters:
other – [in] The other optional object to move the value or lack of value from. The value contained in
othermust be move constructible to the data type expected in this object. Theotherobject will be left in a valid but undefined state upon return.
-
template<class ...Args, std::enable_if_t<std::is_constructible_v<T, Args...>, int> = 0>
inline optional(
)# Constructor: piecewise constructs a new optional object from multiple arguments.
- Parameters:
args – [in] The arguments used to contruct the value contained in the new object. These arguments will be forwarded unmodified to the contained type’s constructor.
-
template<typename U, class ...Args, std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args...>, int> = 0>
inline optional(
)# Constructor: piecewise constructs a new optional object from multiple arguments.
- Parameters:
ilist – [in] The initializer list used to construct the new object.
args – [in] The arguments used to satisfy the initializer list
ilist.
-
template<typename U = value_type, std::enable_if_t<AllowDirectConversion_v<U> && std::is_convertible_v<U, T>, int> = 0>
inline constexpr optional( - U &&value,
Move constructor: moves a value into this object.
- Parameters:
value – [in] The new value to assign to this object. This value must be move constructible to the data type expected in this object. The
valueobject will be left in a vaid but undefined state upon return.
-
~optional() = default#
-
inline optional &operator=(nullopt_t) noexcept#
Null assignment operator. This will clear out any value stored in this object and reset it to its default constructed state.
- Returns:
A reference to this object.
-
template<typename U = value_type, std::enable_if_t<!std::is_same_v<optional, remove_cvref_t<U>> && !(std::is_scalar_v<T> && std::is_same_v<T, std::decay_t<U>>) && std::is_constructible_v<T, U> && std::is_assignable_v<T&, U>, int> = 0>
inline optional &operator=( - U &&value,
Value assignment operator.
- Parameters:
value – [in] The new raw value to assign to this object. This value may be of any type that can be implicitly converted to this object’s contained type.
- Returns:
A reference to this object.
-
template<typename U, std::enable_if_t<AllowUnwrappingAssignment_v<U> && std::is_constructible_v<T, const U&> && std::is_assignable_v<T&, const U&>, int> = 0>
inline optional &operator=(
)# Value assignment operator.
- Parameters:
other – [in] The optional value containing the new value (or lack of value) to assign to this object. The contained value may be of any type that can be implicitly converted to this object’s contained type.
- Returns:
A reference to this object.
-
template<typename U, std::enable_if_t<AllowUnwrappingAssignment_v<U> && std::is_constructible_v<T, U> && std::is_assignable_v<T&, U>, int> = 0>
inline optional &operator=(
)# Move assignment operator.
- Parameters:
other – [in] The optional value containing the new value (or lack of value) to assign to this object. The contained value may be of any type that can be implicitly converted to this object’s contained type. The
otherobject will be in a valid but undefined state upon return.- Returns:
A reference to this object.
-
inline iterator begin() noexcept#
Returns an iterator to the beginning.
If
*thiscontains a value, returns an iterator to the contained value. Otherwise, a past-the-end iterator.- Returns:
Iterator to the contained value if has_value() is
true. Otherwise, a past-the-end iterator.
-
inline const_iterator begin() const noexcept#
Returns an iterator to the beginning.
If
*thiscontains a value, returns an iterator to the contained value. Otherwise, a past-the-end iterator.- Returns:
Iterator to the contained value if has_value() is
true. Otherwise, a past-the-end iterator.
-
inline iterator end() noexcept#
Returns an iterator to the end.
Returns a past-the-end iterator. Equivalent to
return begin() + has_value();.- Returns:
a past-the-end iterator.
-
inline const_iterator end() const noexcept#
Returns an iterator to the end.
Returns a past-the-end iterator. Equivalent to
return begin() + has_value();.- Returns:
a past-the-end iterator.
-
inline constexpr const T *operator->() const#
Access operator. This operation is considered undefined behavior if no value is currently contained in this object.
- Returns:
The address of the contained value.
-
inline constexpr T *operator->()#
Access operator. This operation is considered undefined behavior if no value is currently contained in this object.
- Returns:
The address of the contained value.
-
inline constexpr const T &operator*() const &#
Dereference operator for the contained value. This operation is considered undefined behavior if no value is currently contained in this object.
- Returns:
A const reference to the contained value.
-
inline constexpr T &operator*() &#
Dereference operator for the contained value. This operation is considered undefined behavior if no value is currently contained in this object.
- Returns:
A reference to the contained value.
-
inline constexpr const T &&operator*() const &&#
Dereference-and-move operator for the contained value. This operation is considered undefined behavior if no value is currently contained in this object.
- Returns:
A const reference to the contained value.
-
inline constexpr T &&operator*() &&#
Dereference-and-move operator for the contained value. This operation is considered undefined behavior if no value is currently contained in this object.
- Returns:
A reference to the contained value.
-
inline explicit constexpr operator bool() const noexcept#
Value test operator. Checks if this object currently contains a value.
- Returns:
trueif a value is currently contained in this object andfalseotherwise.
-
inline constexpr bool has_value() const noexcept#
Value test operator. Checks if this object currently contains a value.
- Returns:
trueif a value is currently contained in this object andfalseotherwise.
-
inline constexpr const T &value() const &#
Value retrieval accessor. This will throw
bad_optional_accessif no value is contained in this object (or terminate if exceptions are disabled).- Returns:
A reference to the value contained in this object.
-
inline constexpr T &value() &#
Value retrieval accessor. This will throw
bad_optional_accessif no value is contained in this object (or terminate if exceptions are disabled).- Returns:
A reference to the value contained in this object.
-
inline constexpr const T &&value() const &&#
Value move accessor. This will throw
bad_optional_accessif no value is contained in this object (or terminate if exceptions are disabled). This object will be left in a valid but undefined state.- Returns:
An rvalue-reference to the contained value.
-
inline constexpr T &&value() &&#
Value move accessor. This will throw
bad_optional_accessif no value is contained in this object (or terminate if exceptions are disabled). This object will be left in a valid but undefined state.- Returns:
An rvalue-reference to the contained value.
-
template<typename U>
inline constexpr std::remove_cv_t<T> value_or( - U &&default_value,
Returns the contained value if
*thishas a value, otherwise returnsdefault_value.- Parameters:
default_value – [in] The value to use in case
*thisis empty.- Returns:
The current value if
*thishas a value, ordefault_valueotherwise.
-
template<typename U>
inline constexpr std::remove_cv_t<T> value_or( - U &&default_value,
Returns the contained value if
*thishas a value, otherwise returnsdefault_value.- Parameters:
default_value – [in] The value to use in case
*thisis empty.- Returns:
The current value if
*thishas a value, ordefault_valueotherwise.
-
template<typename F>
inline constexpr auto and_then(F &&f) &# Returns the result of the given function on the contained value if it exists, or an empty optional otherwise.
If
*thiscontains a value, invokesfwith the contained value as an argument, and returns the result of that invocation; otherwise returns an emptyoptional.The return type must be a specialization of
optional(unlike transform()). Otherwise, the program is ill-formed.- Parameters:
f – A suitable function or Callable object that returns an
optional.- Returns:
The result of
for an emptyoptional, as described above.
-
template<typename F>
inline constexpr auto and_then(F &&f) const &# Returns the result of the given function on the contained value if it exists, or an empty optional otherwise.
If
*thiscontains a value, invokesfwith the contained value as an argument, and returns the result of that invocation; otherwise returns an emptyoptional.The return type must be a specialization of
optional(unlike transform()). Otherwise, the program is ill-formed.- Parameters:
f – A suitable function or Callable object that returns an
optional.- Returns:
The result of
for an emptyoptional, as described above.
-
template<typename F>
inline constexpr auto and_then(F &&f) &&# Returns the result of the given function on the contained value if it exists, or an empty optional otherwise.
If
*thiscontains a value, invokesfwith the contained value as an argument, and returns the result of that invocation; otherwise returns an emptyoptional.The return type must be a specialization of
optional(unlike transform()). Otherwise, the program is ill-formed.- Parameters:
f – A suitable function or Callable object that returns an
optional.- Returns:
The result of
for an emptyoptional, as described above.
-
template<typename F>
inline constexpr auto and_then(F &&f) const &&# Returns the result of the given function on the contained value if it exists, or an empty optional otherwise.
If
*thiscontains a value, invokesfwith the contained value as an argument, and returns the result of that invocation; otherwise returns an emptyoptional.The return type must be a specialization of
optional(unlike transform()). Otherwise, the program is ill-formed.- Parameters:
f – A suitable function or Callable object that returns an
optional.- Returns:
The result of
for an emptyoptional, as described above.
-
template<typename F>
inline constexpr auto transform(F &&f) &# Returns an optional containing the transformed contained value if it exists, or an empty optional otherwise.
If
*thiscontains a value, invokesfwith the contained value as an argument, and returns anoptionalthat contains the result of that invocation; otherwise, returns an emptyoptional.The type of contained value in the result must be a non-array object type, and must not be
in_place_tornullopt_t. Otherwise, the program is ill-formed.- Parameters:
f – A suitable function or Callable object whose call signature contains a non-reference type.
- Returns:
An
optionalcontaining the result offor an emptyoptional, as described above.
-
template<typename F>
inline constexpr auto transform(F &&f) const &# Returns an optional containing the transformed contained value if it exists, or an empty optional otherwise.
If
*thiscontains a value, invokesfwith the contained value as an argument, and returns anoptionalthat contains the result of that invocation; otherwise, returns an emptyoptional.The type of contained value in the result must be a non-array object type, and must not be
in_place_tornullopt_t. Otherwise, the program is ill-formed.- Parameters:
f – A suitable function or Callable object whose call signature contains a non-reference type.
- Returns:
An
optionalcontaining the result offor an emptyoptional, as described above.
-
template<typename F>
inline constexpr auto transform(F &&f) &&# Returns an optional containing the transformed contained value if it exists, or an empty optional otherwise.
If
*thiscontains a value, invokesfwith the contained value as an argument, and returns anoptionalthat contains the result of that invocation; otherwise, returns an emptyoptional.The type of contained value in the result must be a non-array object type, and must not be
in_place_tornullopt_t. Otherwise, the program is ill-formed.- Parameters:
f – A suitable function or Callable object whose call signature contains a non-reference type.
- Returns:
An
optionalcontaining the result offor an emptyoptional, as described above.
-
template<typename F>
inline constexpr auto transform(F &&f) const &&# Returns an optional containing the transformed contained value if it exists, or an empty optional otherwise.
If
*thiscontains a value, invokesfwith the contained value as an argument, and returns anoptionalthat contains the result of that invocation; otherwise, returns an emptyoptional.The type of contained value in the result must be a non-array object type, and must not be
in_place_tornullopt_t. Otherwise, the program is ill-formed.- Parameters:
f – A suitable function or Callable object whose call signature contains a non-reference type.
- Returns:
An
optionalcontaining the result offor an emptyoptional, as described above.
-
template<typename F, typename U = T, std::enable_if_t<std::is_invocable_v<F> && std::is_copy_constructible_v<U>, int> = 0>
inline constexpr optional or_else( - F &&f,
Returns the optional itself if it contains a value, or the result of the given function otherwise.
Returns
*thisif it contains a value. Otherwise returns the result off. The program is ill-formed ifremove_cvref_t<std::invoke_result_t<F>>is not the same asoptional<T>.- Parameters:
f – A function or Callable object that returns an
optional<T>.- Returns:
*thisor the result off, as described above.
-
template<typename F, typename U = T, std::enable_if_t<std::is_invocable_v<F> && std::is_move_constructible_v<U>, int> = 0>
inline constexpr optional or_else( - F &&f,
Returns the optional itself if it contains a value, or the result of the given function otherwise.
Returns
*thisif it contains a value. Otherwise returns the result off. The program is ill-formed ifremove_cvref_t<std::invoke_result_t<F>>is not the same asoptional<T>.- Parameters:
f – A function or Callable object that returns an
optional<T>.- Returns:
*thisor the result off, as described above.
- inline void swap(
- optional &other,
Swaps the values or lack of value between this object and another object.
- Parameters:
other – [in] The object to swap the state with this object. If
otherdid not contain a value, this object will not contain a value upon return. If this object does not contain a value,otherwill not contain a value upon return. If both contain a value, those values will be swapped upon return. The contained type must be move constructible and swappable.
-
inline void reset() noexcept#
Destroys any contained value.
If
*thiscontains a value, destroy that value as if byvalue().T::~T(). Otherwise, there are no effects.*thisdoes not contain a value after this call.
-
template<class ...Args>
inline T &emplace(Args&&... args)# Piecewise constructs a new value in this object. Any previous contained value will be destructed and discarded.
- Parameters:
args – [in] Argument list needed to construct the new object.
- Returns:
A reference to this object.
-
template<typename U, class ...Args, std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args...>, int> = 0>
inline T &emplace(
)# Piecewise constructs a new value in this object. Any previous contained value will be destructed and discarded.
- Parameters:
ilist – [in] Initializer list used to construct the new value.
args – [in] Argument list needed to satisfy the initializer list
ilist.
- Returns:
A reference to this object.