Main Content

matlab::data::optional<T>

Templated C++ class representing optional values

Description

Use optional objects to represent values that might or might not exist.

Class Details

Namespace:

matlab::data

Include:

Optional.hpp

Template Parameters

T

Array type, specified as matlab::data::String.

Constructors

Default Constructors

optional()

Copy Constructors

optional(const optional& other)

Description

Creates a shared data copy.

Parameters

const optional& other

Value to copy.

Throws

None

Copy Assignment Operators

optional<T>& operator=(const optional<T>& other)

Description

Assigns a shared data copy.

Parameters

const optional<T>& other

Value to copy.

Returns

optional<T>&

Updated instance.

Throws

None

Move Constructors

optional(optional&& other)

optional(T&& value)

Description

Moves contents of an optional object to a new instance.

Parameters

optional&& other

Value to move.

T&& value

Value of type T to move.
Throws

None

Move Assignment Operators

optional<T>& operator=(optional<T>&& other)

optional<T>& operator=(T&& value)

Description

Assigns the input to this instance.

Parameters

optional<T>&& other

T&& value

Value to move.

Returns

optional<T>&

Updated instance.

Throws

None

Other Operators

operator=

optional<T>& operator=(nullopt_t)

optional<T>& operator=(const optional<T>& other)

optional<T>& operator=(optional<T>&& other)

optional<T>& operator=(T&& value)

optional<T>& operator=(const T& value)

Description

Assignment operators.

Returns

optional<T>&

Updated instance.

Throws

None

operator->

const T* operator->() const

T* operator->()

Returns

const T*

T*

Pointer to the element.

Throws

std::runtime_error

optional object does not contain a value.

operator*

const T& operator*() const

T& operator*()

Returns

const T&

T&

Reference to the element.
Throws

std::runtime_error

optional object does not contain a value.

operator T

operator T() const
Description

Cast optional<T> value to T.

Returns

operator

Value contained in optional<T>, if it exists.

Throws

std::runtime_error

There is no value.

Member Functions

bool

explicit operator bool() const
Description

Check whether object contains a value.

Returns

operator

True, if object contains a value.

Throws

None

has_value

bool has_value() const
Description

Check whether object contains a value.

Returns

bool

True, if object contains a value.

Throws

None

swap

void swap(optional &other)
Description

Swap value of this optional instance with value contained in the parameter.

Parameters

optional &other

Value to swap.

Throws

None

reset

void reset()
Description

Reset optional value to missing

Throws

None

Version History

Introduced in R2017b