diff --git a/doc/01_quick_start.qbk b/doc/01_quick_start.qbk index 03f6eee..53a8e67 100644 --- a/doc/01_quick_start.qbk +++ b/doc/01_quick_start.qbk @@ -127,6 +127,20 @@ The second line works already, this is the capability of Boost.Tuple library, bu It works because inside `boost::tie` a move-assignment from `T` is invoked on `optional`, which internally calls a move-constructor of `T`. [/endsect] +[heading 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`. You want to make a histogram showing how many times each choice was made. You can use an `std::map`: + + std::map, int> choices; + + for (int i = 0; i < LIMIT; ++i) { + boost::optional choice = readChoice(); + ++choices[choice]; + } + +This works because `optional` 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` can assume is {`boost::none`, -2147483648, -2147483647, ..., -1, 0, 1, ..., 2147483647} (assuming a 32-bit `int`). +[/endsect] + [endsect] diff --git a/doc/html/boost_optional/quick_start.html b/doc/html/boost_optional/quick_start.html index dbe86fc..5434009 100644 --- a/doc/html/boost_optional/quick_start.html +++ b/doc/html/boost_optional/quick_start.html @@ -244,6 +244,36 @@ from T is invoked on optional<T>, which internally calls a move-constructor of T.

+

+ + 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: +

+
std::map<boost::optional<int>, int> choices;
+
+for (int i = 0; i < LIMIT; ++i) {
+  boost::optional<int> choice = readChoice();
+  ++choices[choice];
+}
+
+

+ 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). +

diff --git a/doc/html/index.html b/doc/html/index.html index 3508307..0ae3385 100644 --- a/doc/html/index.html +++ b/doc/html/index.html @@ -112,7 +112,7 @@
- +

Last revised: June 04, 2014 at 16:06:37 GMT

Last revised: June 04, 2014 at 21:00:26 GMT