Quick Links: |
Provide a standard-compliant allocator aware optional type. More...
bsl::optional | template class for optional objects |
bsl::optional<TYPE>
, that implements a notion of object that may or may not contain a TYPE
value. This template class also provides an interface to check if the optional object contains a value or not, as well as a contextual conversion to bool
. If an optional object is engaged, i.e. contains a value, it will evaluate to true
when converted to bool
; otherwise, it will evaluate to false
when converted to bool
. TYPE
or another engaged optional object, or if the value was created using the emplace
method. Other types of assignment and initialization (including default initialization), as well as calling reset
method will result in the optional object being disengaged. TYPE
has value-semantics, then so will the type bsl::optional<TYPE>
. Two homogeneous optional objects have the same value if their underlying (non-null) TYPE
values are the same, or both are null. TYPE
that is managed by a bsl::optional<TYPE>
object is created in-*place*. Consequently, the template parameter TYPE
must be a complete type when the class is instantiated. bsl::optional
also supports conversion between optional types for which the underlying types are convertible, i.e., for heterogeneous copy construction, copy assignment, and equality comparison (e.g., between int
and double
); attempts at conversion between incompatible types, such as int
and bsl::string
, will fail to compile. value_type
objects it manages during its lifetime. $ grep -n Section: bslstl_optional.h
optional
int
object: bsl::optional<int> optionalInt; assert(!optionalInt.has_value());
int
object the value 123 (making it non-null): Finally, reset the object to its default constructed state (i.e., null): is_constructible
but the exact creation will be done using allocation construction that will invoke an allocator-extended constructor for allocator-aware types. If the value_type
is constructible from the assignment/constructor argument, but doesn't have a corresponding allocator-extended constructor, the overload selection may not be be correct. optional<const TYPE>
is fully supported in C++11 and onwards. However, due to limitations of MovableRef<const TYPE>
, C++03 support for const value_type
s is limited and move semantics of such an optional
in C++03 will not work.