mirror of
https://github.com/boostorg/optional.git
synced 2025-07-29 03:57:20 +02:00
Cleaned up docs
This commit is contained in:
@ -12,7 +12,7 @@
|
||||
|
||||
[section Quick Start]
|
||||
|
||||
[heading Optional return values]
|
||||
[section Optional return values]
|
||||
|
||||
Let's write and use a converter function that converts an a `std::string` to an `int`. It is possible that for a given string (e.g. `"cat"`) there exist no value of type `int` capable of representing the conversion result. We do not consider such situation an error. We expect that the converter can be used only to check if the conversion is possible. A natural signature for this function can be:
|
||||
|
||||
@ -62,9 +62,9 @@ This uses the `atoi`-like approach to conversions: if `text` does not represent
|
||||
|
||||
Observe the two return statements. `return i` uses the converting constructor that can create `optional<T>` from `T`. Thus constructed optional object is initialized and its value is a copy of `i`. The other return statement uses another converting constructor from a special tag `boost::none`. It is used to indicate that we want to create an uninitialized optional object.
|
||||
|
||||
[/endsect]
|
||||
[endsect]
|
||||
|
||||
[heading Optional automatic variables]
|
||||
[section Optional automatic variables]
|
||||
|
||||
We could write function `convert` in a slightly different manner, so that it has a single `return`-statement:
|
||||
|
||||
@ -80,9 +80,9 @@ We could write function `convert` in a slightly different manner, so that it has
|
||||
}
|
||||
|
||||
The default constructor of `optional` creates an unitialized optional object. Unlike with `int`s you cannot have an `optional<int>` in an indeterminate state. Its state is always well defined. Instruction `ans = i` initializes the optional object. It uses the assignment from `int`. In general, for `optional<T>`, when an assignment from `T` is invoked, it can do two things. If the optional object is not initialized our case here), it initializes it with `T`'s copy constructor. If the optional object is already initialized, it assigns the new value to it using `T`'s copy assignment.
|
||||
[/endsect]
|
||||
[endsect]
|
||||
|
||||
[heading Optional data members]
|
||||
[section Optional data members]
|
||||
|
||||
Suppose we want to implement a ['lazy load] optimization. This is because we do not want to perform an expensive initialization of our `Resource` until (if at all) it is really used. We can do it this way:
|
||||
|
||||
@ -106,9 +106,9 @@ Suppose we want to implement a ['lazy load] optimization. This is because we do
|
||||
|
||||
[note Function `emplace` is only available on compilers that support rvalue references and variadic templates. If your compiler does not support these features and you still need to avoid any move-constructions, use [link boost_optional.tutorial.in_place_factories In-Place Factories].]
|
||||
|
||||
[/endsect]
|
||||
[endsect]
|
||||
|
||||
[heading Bypassing unnecessary default construction]
|
||||
[section Bypassing unnecessary default construction]
|
||||
|
||||
Suppose we have class `Date`, which does not have a default constructor: there is no good candidate for a default date. We have a function that returns two dates in form of a `boost::tuple`:
|
||||
|
||||
@ -125,9 +125,9 @@ The second line works already, this is the capability of Boost.Tuple library, bu
|
||||
boost::tie(begin, end) = getPeriod();
|
||||
|
||||
It works because inside `boost::tie` a move-assignment from `T` is invoked on `optional<T>`, which internally calls a move-constructor of `T`.
|
||||
[/endsect]
|
||||
[endsect]
|
||||
|
||||
[heading Storage in containers]
|
||||
[section Storage in containers]
|
||||
|
||||
Suppose you want to ask users to choose some number (an `int`). One of the valid responses is to choose nothing, which is represented by an uninitialized `optional<int>`. You want to make a histogram showing how many times each choice was made. You can use an `std::map`:
|
||||
|
||||
@ -139,7 +139,7 @@ Suppose you want to ask users to choose some number (an `int`). One of the valid
|
||||
}
|
||||
|
||||
This works because `optional<T>` is `LessThanComparable` whenever `T` is `LessThanComparable`. In this case the state of being uninitialized is treated as a yet another value of `T`, which is compared less than any value of `T`. So the set of values that type `optional<T>` can assume is {`boost::none`, -2147483648, -2147483647, ..., -1, 0, 1, ..., 2147483647} (assuming a 32-bit `int`).
|
||||
[/endsect]
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
Reference in New Issue
Block a user