Template Class Optional

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 Types

using value_type = T

Public Functions

inline Optional() noexcept

Default constructor that initializes an empty Optional

inline Optional(NullOptT) noexcept

Constructs an empty Optional using the specified NullOptT tag.

inline Optional(const Optional &that)

Copy constructor. If the other Optional contains a value, it will be copied to this Optional.

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 another Optional of a different type.

This constructor allows for converting between Optional objects of different types, provided that the contained type T can be constructed from the type U of the source Optional.

Template Parameters:
  • U – The contained type of the source Optional.

  • std::is_constructible<T, const – U &>::value Ensures that this constructor is only available if T can be constructed from a constant reference to U.

Parameters:

that – The source Optional object to be copied from.

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 another Optional of a different type.

This constructor allows for converting between Optional objects of different types using move semantics, provided that the contained type T can be constructed from the type U of the source Optional using move construction.

It is marked noexcept if the move construction of T from U 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 Parameters:
  • U – The contained type of the source Optional.

  • std::is_constructible<T, U – &&>::value Ensures that this constructor is only available if T can be move-constructed from type U.

Parameters:

that – The source Optional object to be moved from.

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:

  1. When the passed argument is of type detail::InPlaceT, which is used to signal in-place construction.

  2. When the passed argument is another Optional object, preventing accidental nesting of Optional 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 argument U.

  • !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 the Optional 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 type T can be constructed from the provided argument pack AA....

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 pack AA....

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()
inline Optional &operator=(NullOptT) noexcept

Comparison operators below.

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)
inline Optional &operator=(const Optional &that)
inline Optional &operator=(Optional &&that)
inline Optional &operator=(const T &value)
inline Optional &operator=(T &&value)
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 the Optional 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.

inline const T &value() const

Returns the contained value (const version).

Throws:

std::runtime_error – If the Optional does not contain a value.

Returns:

A const reference to the contained value.

inline T *operator->()
inline const T *operator->() const
inline T &operator*()
inline const T &operator*() const