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]
2014-05-05 19:08:11 +02:00
[copyright 2014 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]]
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
[def __OPTIONAL_POINTEE__ [@../../../utility/OptionalPointee.html OptionalPointee]]
[def __COPY_CONSTRUCTIBLE__ [@../../../utility/CopyConstructible.html Copy Constructible]]
[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]]
[def __SGI_DEFAULT_CONSTRUCTIBLE__ [@http://www.sgi.com/tech/stl/DefaultConstructible.html Default Constructible]]
[/ 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-06-03 17:36:18 +02:00
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 different. Also suppose that any possible value of type `int` is a valid value for `"MaxValue"`, so we cannot jut use `-1` to represent the absence of the parameter in the config file.
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>
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();
}
2014-06-06 00:53:15 +02:00
[endsect]
2014-06-04 18:13:06 +02:00
[include 01_quick_start.qbk]
[section Tutorial]
[include 12_motivation.qbk]
[include 13_development.qbk]
[include 14_optional_references.qbk]
[include 15_in_place_factories.qbk]
[include 16_optional_bool.qbk]
[include 17_exception_safety.qbk]
[include 18_type_requirements.qbk]
[endsect]
[section Reference]
[include 20_reference.qbk]
[endsect]
2014-06-03 17:36:18 +02:00
[include 90_dependencies.qbk]
[include 91_acknowledgments.qbk]
2007-05-29 06:40:25 +00:00