Template Class Optional
Defined in File Optional.hpp
Class Documentation
-
template<class T>
class Optional A container object that may or may not contain a value of a given type.
This is a simplified version of the
std::optional
type introduced in C++17. It provides a mechanism to represent non-value states without resorting to pointers, dynamic allocation, or custom ‘null’ values.- Template Parameters:
T – The type of the value to be stored.
Public Functions
-
inline Optional(const Optional &that)
Copy constructor. If the other
Optional
contains a value, it will be copied to thisOptional
.
-
inline Optional(Optional &&that) noexcept(std::is_nothrow_move_constructible<T>::value)
Move constructor.
-
template<typename U, detail::EnableIf_t<std::is_constructible<T, const U&>::value, int> = 0>
inline Optional(const Optional<U> &that) Constructs an
Optional
object by copying the contents of anotherOptional
of a different type.This constructor allows for converting between
Optional
objects of different types, provided that the contained typeT
can be constructed from the typeU
of the sourceOptional
.
-
template<typename U, detail::EnableIf_t<std::is_constructible<T, U&&>::value, int> = 0>
inline Optional(Optional<U> &&that) noexcept(std::is_nothrow_constructible<T, U&&>::value) Constructs an
Optional
object by moving the contents of anotherOptional
of a different type.This constructor allows for converting between
Optional
objects of different types using move semantics, provided that the contained typeT
can be constructed from the typeU
of the sourceOptional
using move construction.It is marked
noexcept
if the move construction ofT
fromU
does not throw exceptions.After the move, the source
Optional
retains its state (i.e., whether it has a value or not) as per the C++17 standard.
-
template<class U, detail::EnableIf_t<std::is_constructible<T, U&&>::value && !std::is_same<typename std::decay<U>::type, detail::InPlaceT>::value && !std::is_same<typename std::decay<U>::type, Optional<U>>::value, int> = 0>
inline Optional(U &&that) Constructs an
Optional
object with a contained value by forwarding the given argument.This constructor is designed for direct value initialization of the
Optional
object’s contained value. It employs perfect forwarding to ensure efficiency and flexibility, allowing for both lvalue and rvalue arguments.Importantly, this constructor is selectively disabled in the following scenarios:
When the passed argument is of type
detail::InPlaceT
, which is used to signal in-place construction.When the passed argument is another
Optional
object, preventing accidental nesting ofOptional
objects.
The use of SFINAE (
detail::EnableIf_t
) ensures that this constructor is only available under appropriate conditions.- Template Parameters:
U – The type of the argument used to initialize the contained value.
std::is_constructible<T, U – &&>::value Ensures that the contained type
T
can be constructed from the argumentU
.!std::is_same<typename – std::decay<U>::type, detail::InPlaceT>::value Ensures that the constructor is disabled when the argument is
detail::InPlaceT
.!std::is_same<typename – std::decay<U>::type, Optional
- Parameters:
that –
-
template<class ...AA, detail::EnableIf_t<std::is_constructible<T, AA...>::value, int> = 0>
inline Optional(detail::InPlaceT, AA&&... args) Constructs an
Optional
object with an in-place constructed contained value.This constructor is designed for in-place construction of the
Optional
object’s contained value. The purpose of in-place construction is to construct the value directly within the storage of theOptional
object, eliminating the need for temporary objects and providing better performance in certain scenarios.The
detail::InPlaceT
tag is used to distinguish this constructor from other overloads and to signal in-place construction.The use of SFINAE (
detail::EnableIf_t
) ensures that this constructor is only available when the contained typeT
can be constructed from the provided argument packAA...
.- Template Parameters:
AA – The types of the arguments used for in-place construction of the contained value.
std::is_constructible<T, AA...>::value – Ensures that the contained type
T
can be constructed from the argument packAA...
.
- Parameters:
std::ignore – A tag that indicates in-place construction.
args... – The arguments used for in-place construction of the
Optional
object’s contained value.
-
inline ~Optional()
-
template<typename U>
inline detail::EnableIf_t<std::is_assignable<T&, const U&>::value, Optional&> operator=(const Optional<U> &that)
-
template<typename U>
inline detail::EnableIf_t<std::is_assignable<T&, U&&>::value, Optional&> operator=(Optional<U> &&that)
-
template<class ...AA, detail::EnableIf_t<std::is_constructible<T, AA...>::value, int> = 0>
inline T &emplace(AA&&... args)
-
inline void reset() noexcept
Resets the
Optional
to its default (empty) state.If the
Optional
contains a value, the value is destroyed and theOptional
becomes empty.
-
inline void swap(Optional &that)
Swaps the contents of two
Optional
objects.If both objects have values, their values are swapped. If only one object has a value, the value is moved to the other object.
- Parameters:
that – Another
Optional
object of the same type.
-
inline bool hasValue() const
Checks if the
Optional
contains a value.- Returns:
true if the
Optional
has a value, false otherwise.
-
inline explicit operator bool() const
Conversion to bool that checks if the
Optional
contains a value.- Returns:
true if the
Optional
has a value, false otherwise.
-
inline T &value()
Returns the contained value.
- Throws:
std::runtime_error – If the
Optional
does not contain a value.- Returns:
A reference to the contained value.