2007-05-29 06:40:25 +00:00
[library Boost.Optional
[quickbook 1.4]
[authors [Cacciola Carballal, Fernando Luis]]
[copyright 2003-2007 Fernando Luis Cacciola Carballal]
2021-11-20 02:00:30 +01:00
[copyright 2014-2021 Andrzej Krzemieński]
2007-05-29 06:40:25 +00:00
[category miscellaneous]
[id optional]
[dirname optional]
[purpose
Discriminated-union wrapper for optional values
]
[source-mode c++]
[license
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
[@http://www.boost.org/LICENSE_1_0.txt])
]
]
[/ Macros will be used for links so we have a central place to change them ]
[/ Cited Boost resources ]
[def __BOOST_VARIANT__ [@../../../variant/index.html Boost.Variant]]
2014-06-08 16:23:35 +02:00
[def __BOOST_TUPLE__ [@../../../tuple/index.html Boost.Tuple]]
Merge in documentation fixes. Apart from the change to optional's documenation
Jamfile, which I included by mistake.
Fixes #1659, #1661, #1684, #1685, 1687, #1690, #1801
I wrote about this at:
http://lists.boost.org/Archives/boost/2008/04/136405.php
Merged revisions 44585-44806 via svnmerge from
https://svn.boost.org/svn/boost/branches/doc
........
r44585 | danieljames | 2008-04-19 16:25:27 +0100 (Sat, 19 Apr 2008) | 2 lines
Fix broken link to vacpp in bjam docs. Refs #1512
........
r44586 | danieljames | 2008-04-19 16:27:36 +0100 (Sat, 19 Apr 2008) | 2 lines
Fix broken link to bcpp in bjam docs. Refs #1513
........
r44587 | danieljames | 2008-04-19 16:33:58 +0100 (Sat, 19 Apr 2008) | 2 lines
DateTime documentation - Fix a link to the serialization library. Refs #1659
........
r44588 | danieljames | 2008-04-19 16:35:36 +0100 (Sat, 19 Apr 2008) | 2 lines
Fix some links in interprocess & intrusive. Refs #1661
........
r44589 | danieljames | 2008-04-19 16:37:39 +0100 (Sat, 19 Apr 2008) | 2 lines
Fix some links in the python docs. Refs #1684.
........
r44590 | danieljames | 2008-04-19 16:38:29 +0100 (Sat, 19 Apr 2008) | 2 lines
Work around a quickbook bug which is affecting the python docs. Refs #1684.
........
r44591 | danieljames | 2008-04-19 16:39:34 +0100 (Sat, 19 Apr 2008) | 2 lines
Fix a broken link in the numeric conversion docs. Refs #1685
........
r44592 | danieljames | 2008-04-19 16:40:45 +0100 (Sat, 19 Apr 2008) | 2 lines
Fix some links in the optional docs. Refs #1687
........
r44593 | danieljames | 2008-04-19 16:42:09 +0100 (Sat, 19 Apr 2008) | 2 lines
Fix link to the hash documentation from bimap. Refs #1690
........
r44599 | danieljames | 2008-04-19 18:07:33 +0100 (Sat, 19 Apr 2008) | 2 lines
Fix a typo in the format library. Refs #1801
........
r44600 | danieljames | 2008-04-19 19:20:59 +0100 (Sat, 19 Apr 2008) | 1 line
Initialise svnmerge.
........
r44641 | danieljames | 2008-04-20 18:59:47 +0100 (Sun, 20 Apr 2008) | 2 lines
Fix the lincense url in shared container iterator documentation.
........
r44642 | danieljames | 2008-04-20 19:00:00 +0100 (Sun, 20 Apr 2008) | 2 lines
Fix image link in the mpi documentation.
........
r44643 | danieljames | 2008-04-20 19:00:11 +0100 (Sun, 20 Apr 2008) | 2 lines
Fix a typo in the spirit docs.
........
r44644 | danieljames | 2008-04-20 19:00:23 +0100 (Sun, 20 Apr 2008) | 2 lines
Escape the slash so that quickbook doesn't think it the start of an italic section, and mess up the link. Refs #1844
........
r44647 | danieljames | 2008-04-20 19:39:47 +0100 (Sun, 20 Apr 2008) | 2 lines
Fix another typo in spirit docs.
........
[SVN r44807]
2008-04-27 07:39:49 +00:00
[def __BOOST_TRIBOOL__ [@../../../../doc/html/tribool.html boost::tribool]]
2007-05-29 06:40:25 +00:00
2014-06-14 22:49:37 +02:00
[def __OPTIONAL_POINTEE__ [@../../../utility/OptionalPointee.html `OptionalPointee`]]
[def __COPY_CONSTRUCTIBLE__ [@../../../utility/CopyConstructible.html `CopyConstructible`]]
2014-06-16 14:23:34 +02:00
[def __MOVE_CONSTRUCTIBLE__ `MoveConstructible`]
2007-05-29 06:40:25 +00:00
[def __FUNCTION_EQUAL_POINTEES__ [@../../../utility/OptionalPointee.html#equal `equal_pointees()`]]
[def __FUNCTION_LESS_POINTEES__ [@../../../utility/OptionalPointee.html#less `less_pointees()`]]
[def __IN_PLACE_FACTORY_HPP__ [@../../../../boost/utility/in_place_factory.hpp in_place_factory.hpp]]
[def __TYPED_IN_PLACE_FACTORY_HPP__ [@../../../../boost/utility/typed_in_place_factory.hpp typed_in_place_factory.hpp]]
[/ Other web resources ]
[def __HASKELL__ [@http://www.haskell.org/ Haskell]]
2014-06-06 23:24:43 +02:00
[def __SGI_DEFAULT_CONSTRUCTIBLE__ [@http://www.sgi.com/tech/stl/DefaultConstructible.html `DefaultConstructible`]]
2014-06-08 16:23:35 +02:00
[def __SGI_LESS_THAN_COMPARABLE__ [@http://www.sgi.com/tech/stl/LessThanComparable.html `LessThanComparable`]]
[def __SGI_EQUALITY_COMPARABLE__ [@http://www.sgi.com/tech/stl/EqualityComparable.html `EqualityComparable`]]
2014-06-18 15:01:52 +02:00
[def __SGI_GENERATOR__ [@http://www.sgi.com/tech/stl/Generator.html `Generator`]]
2007-05-29 06:40:25 +00:00
[/ Icons ]
[def __SPACE__ [$images/space.png]]
[def __GO_TO__ [$images/callouts/R.png]]
2014-06-06 00:53:15 +02:00
[section Introduction]
2014-06-03 17:36:18 +02:00
Class template `optional` is a wrapper for representing 'optional' (or 'nullable') objects who may not (yet) contain a valid value. Optional objects offer full value semantics; they are good for passing by value and usage inside STL containers. This is a header-only library.
2007-05-29 06:40:25 +00:00
2014-06-04 18:13:06 +02:00
[heading Problem]
2014-11-12 08:42:40 +05:30
Suppose we want to read a parameter form a config file which represents some integral value, let's call it `"MaxValue"`. It is possible that this parameter is not specified; such situation is no error. It is valid to not specify the parameter and in that case the program is supposed to behave slightly differently. Also, suppose that any possible value of type `int` is a valid value for `"MaxValue"`, so we cannot just use `-1` to represent the absence of the parameter in the config file.
2014-06-03 17:36:18 +02:00
2014-06-04 18:13:06 +02:00
[heading Solution]
2014-06-03 17:36:18 +02:00
This is how you solve it with `boost::optional`:
#include <boost/optional.hpp>
2020-12-18 11:13:47 -06:00
2014-06-03 17:36:18 +02:00
boost::optional<int> getConfigParam(std::string name); // return either an int or a `not-an-int`
2007-05-29 06:40:25 +00:00
2014-06-03 17:36:18 +02:00
int main()
{
if (boost::optional<int> oi = getConfigParam("MaxValue")) // did I get a real int?
runWithMax(*oi); // use my int
else
runWithNoMax();
}
2020-12-18 11:13:47 -06:00
2014-06-06 00:53:15 +02:00
[endsect]
2014-06-04 18:13:06 +02:00
[include 01_quick_start.qbk]
[section Tutorial]
2014-06-08 16:23:35 +02:00
[include 10_motivation.qbk]
[include 11_development.qbk]
[include 12_when_to_use.qbk]
[include 13_relational_operators.qbk]
2015-01-21 00:10:51 +01:00
[include 14_io.qbk]
[include 15_optional_references.qbk]
[include 16_in_place_factories.qbk]
2016-09-17 01:25:12 +02:00
[include 17_gotchas.qbk]
2015-01-21 00:10:51 +01:00
[include 18_exception_safety.qbk]
[include 19_type_requirements.qbk]
[include 1A_on_performance.qbk]
2014-06-04 18:13:06 +02:00
[endsect]
2015-03-09 21:56:25 +01:00
[section:reference Reference]
[include 21_ref_none.qbk]
[include 22_ref_bad_optional_access.qbk]
[include 23_ref_optional_io.qbk]
[include 24_ref_optional_fwd.qbk]
[section Header <boost/optional/optional.hpp>]
[include 27_ref_optional_synopsis.qbk]
[include 28_ref_optional_semantics.qbk]
[endsect]
[include 29_ref_optional_convenience.qbk]
2014-06-04 18:13:06 +02:00
[endsect]
2014-06-03 17:36:18 +02:00
[include 90_dependencies.qbk]
2015-01-12 17:37:15 +01:00
[include 91_relnotes.qbk]
[include 92_acknowledgments.qbk]