2007-05-29 06:40:25 +00:00
< html >
< head >
2020-10-24 17:17:10 +02:00
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
2014-06-06 00:53:15 +02:00
< title > Boost.Optional</ title >
2014-04-12 20:54:37 +02:00
< link rel = "stylesheet" href = "../../../../doc/src/boostbook.css" type = "text/css" >
2018-03-24 00:04:15 +01:00
< meta name = "generator" content = "DocBook XSL Stylesheets Vsnapshot" >
2014-06-06 00:53:15 +02:00
< link rel = "home" href = "index.html" title = "Boost.Optional" >
2014-06-04 18:13:06 +02:00
< link rel = "next" href = "boost_optional/quick_start.html" title = "Quick Start" >
2007-05-29 06:40:25 +00:00
</ head >
< body bgcolor = "white" text = "black" link = "#0000FF" vlink = "#840084" alink = "#0000FF" >
2008-02-14 17:43:52 +00:00
< table cellpadding = "2" width = "100%" >< tr >
< td valign = "top" >< img alt = "Boost C++ Libraries" width = "277" height = "86" src = "../../../../boost.png" ></ td >
2009-02-12 14:01:48 +00:00
< td align = "center" >< a href = "../../../../index.html" > Home</ a ></ td >
2011-04-06 20:54:18 +00:00
< td align = "center" >< a href = "../../../../libs/libraries.htm" > Libraries</ a ></ td >
2009-02-12 14:01:48 +00:00
< td align = "center" >< a href = "http://www.boost.org/users/people.html" > People</ a ></ td >
< td align = "center" >< a href = "http://www.boost.org/users/faq.html" > FAQ</ a ></ td >
2008-02-14 17:43:52 +00:00
< td align = "center" >< a href = "../../../../more/index.htm" > More</ a ></ td >
</ tr ></ table >
2007-05-29 06:40:25 +00:00
< hr >
2014-06-04 18:13:06 +02:00
< div class = "spirit-nav" >< a accesskey = "n" href = "boost_optional/quick_start.html" >< img src = "../../../../doc/src/images/next.png" alt = "Next" ></ a ></ div >
2011-04-06 20:54:18 +00:00
< div class = "chapter" >
2007-05-29 06:40:25 +00:00
< div class = "titlepage" >< div >
< div >< h2 class = "title" >
2014-06-06 00:53:15 +02:00
< a name = "optional" ></ a > Boost.Optional</ h2 ></ div >
2007-05-29 06:40:25 +00:00
< div >< div class = "author" >< h3 class = "author" >
< span class = "firstname" > Fernando Luis</ span > < span class = "surname" > Cacciola Carballal</ span >
</ h3 ></ div ></ div >
2020-10-24 17:17:10 +02:00
< div >< p class = "copyright" > Copyright © 2003-2007 Fernando Luis Cacciola Carballal</ p ></ div >
2021-11-20 02:00:30 +01:00
< div >< p class = "copyright" > Copyright © 2014-2021 Andrzej Krzemieński</ p ></ div >
2011-04-06 20:54:18 +00:00
< div >< div class = "legalnotice" >
2013-02-16 19:42:42 +00:00
< a name = "optional.legal" ></ a >< p >
2007-05-29 06:40:25 +00:00
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at < a href = "http://www.boost.org/LICENSE_1_0.txt" target = "_top" > http://www.boost.org/LICENSE_1_0.txt</ a > )
</ p >
</ div ></ div >
</ div ></ div >
< div class = "toc" >
< p >< b > Table of Contents</ b ></ p >
2014-04-12 20:54:37 +02:00
< dl class = "toc" >
2014-06-06 00:53:15 +02:00
< dt >< span class = "section" >< a href = "index.html#optional.introduction" > Introduction</ a ></ span ></ dt >
2014-06-04 18:13:06 +02:00
< dt >< span class = "section" >< a href = "boost_optional/quick_start.html" > Quick Start</ a ></ span ></ dt >
2014-06-06 00:53:15 +02:00
< dd >< dl >
< dt >< span class = "section" >< a href = "boost_optional/quick_start.html#boost_optional.quick_start.optional_return_values" > Optional
return values</ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/quick_start/optional_automatic_variables.html" > Optional
automatic variables</ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/quick_start/optional_data_members.html" > Optional
data members</ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/quick_start/bypassing_unnecessary_default_construction.html" > Bypassing
unnecessary default construction</ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/quick_start/storage_in_containers.html" > Storage
in containers</ a ></ span ></ dt >
</ dl ></ dd >
2014-06-04 18:13:06 +02:00
< dt >< span class = "section" >< a href = "optional/tutorial.html" > Tutorial</ a ></ span ></ dt >
2014-06-03 17:36:18 +02:00
< dd >< dl >
2014-06-06 00:53:15 +02:00
< dt >< span class = "section" >< a href = "optional/tutorial.html#boost_optional.tutorial.motivation" > Motivation</ a ></ span ></ dt >
2014-06-04 18:13:06 +02:00
< dt >< span class = "section" >< a href = "boost_optional/tutorial/design_overview.html" > Design Overview</ a ></ span ></ dt >
2014-06-08 16:23:35 +02:00
< dt >< span class = "section" >< a href = "boost_optional/tutorial/when_to_use_optional.html" > When to
use Optional</ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/tutorial/relational_operators.html" > Relational
operators</ a ></ span ></ dt >
2015-01-21 00:10:51 +01:00
< dt >< span class = "section" >< a href = "boost_optional/tutorial/io_operators.html" > IO operators</ a ></ span ></ dt >
2014-06-04 18:13:06 +02:00
< dt >< span class = "section" >< a href = "boost_optional/tutorial/optional_references.html" > Optional
references</ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/tutorial/in_place_factories.html" > In-Place
Factories</ a ></ span ></ dt >
2016-09-17 01:25:12 +02:00
< dt >< span class = "section" >< a href = "boost_optional/tutorial/gotchas.html" > Gotchas</ a ></ span ></ dt >
2014-06-04 18:13:06 +02:00
< dt >< span class = "section" >< a href = "boost_optional/tutorial/exception_safety_guarantees.html" > Exception
Safety Guarantees</ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/tutorial/type_requirements.html" > Type requirements</ a ></ span ></ dt >
2014-12-06 19:27:53 +01:00
< dt >< span class = "section" >< a href = "boost_optional/tutorial/performance_considerations.html" > Performance
considerations</ a ></ span ></ dt >
2014-06-03 17:36:18 +02:00
</ dl ></ dd >
2014-06-04 18:13:06 +02:00
< dt >< span class = "section" >< a href = "optional/reference.html" > Reference</ a ></ span ></ dt >
2014-06-03 17:36:18 +02:00
< dd >< dl >
2015-03-09 21:56:25 +01:00
< dt >< span class = "section" >< a href = "optional/reference.html#boost_optional.reference.header__boost_none_hpp_" > Header
< boost/none.hpp> </ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/reference/header__boost_optional_bad_optional_access_hpp_.html" > Header
< boost/optional/bad_optional_access.hpp> </ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/reference/io_header.html" > Header < boost/optional/optional_io.hpp> </ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/reference/header__boost_optional_optional_fwd_hpp_.html" > Header
< boost/optional/optional_fwd.hpp> </ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "optional/reference/header__boost_optional_optional_hpp_.html" > Header
< boost/optional/optional.hpp> </ a ></ span ></ dt >
< dt >< span class = "section" >< a href = "boost_optional/reference/header__boost_optional_hpp_.html" > Header
< boost/optional.hpp> </ a ></ span ></ dt >
2014-06-03 17:36:18 +02:00
</ dl ></ dd >
2007-05-29 06:40:25 +00:00
< dt >< span class = "section" >< a href = "boost_optional/dependencies_and_portability.html" > Dependencies
and Portability</ a ></ span ></ dt >
2014-06-06 00:53:15 +02:00
< dd >< dl >
< dt >< span class = "section" >< a href = "boost_optional/dependencies_and_portability.html#boost_optional.dependencies_and_portability.dependencies" > Dependencies</ a ></ span ></ dt >
2016-10-28 00:01:13 +02:00
< dt >< span class = "section" >< a href = "boost_optional/dependencies_and_portability/emplace_operations_in_older_compilers.html" > Emplace
operations in older compilers</ a ></ span ></ dt >
2014-06-06 00:53:15 +02:00
< dt >< span class = "section" >< a href = "boost_optional/dependencies_and_portability/optional_reference_binding.html" > Optional
Reference Binding</ a ></ span ></ dt >
</ dl ></ dd >
2015-01-12 17:37:15 +01:00
< dt >< span class = "section" >< a href = "boost_optional/relnotes.html" > Release Notes</ a ></ span ></ dt >
2014-06-18 15:01:52 +02:00
< dt >< span class = "section" >< a href = "boost_optional/acknowledgements.html" > Acknowledgements</ a ></ span ></ dt >
2007-05-29 06:40:25 +00:00
</ dl >
</ div >
2014-06-06 00:53:15 +02:00
< div class = "section" >
< div class = "titlepage" >< div >< div >< h2 class = "title" style = "clear: both" >
< a name = "optional.introduction" ></ a >< a class = "link" href = "index.html#optional.introduction" title = "Introduction" > Introduction</ a >
</ h2 ></ div ></ div ></ div >
2007-05-29 06:40:25 +00:00
< p >
2014-06-06 00:53:15 +02:00
Class template < code class = "computeroutput" >< span class = "identifier" > optional</ span ></ code > 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.
</ p >
< h4 >
< a name = "optional.introduction.h0" ></ a >
< span class = "phrase" >< a name = "optional.introduction.problem" ></ a ></ span >< a class = "link" href = "index.html#optional.introduction.problem" > Problem</ a >
</ h4 >
2014-06-03 17:36:18 +02:00
< p >
2014-06-06 00:53:15 +02:00
Suppose we want to read a parameter form a config file which represents some
integral value, let's call it < code class = "computeroutput" >< span class = "string" > "MaxValue"</ span ></ code > .
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
2014-09-12 11:57:44 +02:00
to behave slightly differently. Also, suppose that any possible value of type
2014-11-22 01:18:25 +01:00
< code class = "computeroutput" >< span class = "keyword" > int</ span ></ code > is a valid value for < code class = "computeroutput" >< span class = "string" > "MaxValue"</ span ></ code > , so we cannot just use
< code class = "computeroutput" >< span class = "special" > -</ span >< span class = "number" > 1</ span ></ code >
to represent the absence of the parameter in the config file.
2014-06-06 00:53:15 +02:00
</ p >
< h4 >
< a name = "optional.introduction.h1" ></ a >
< span class = "phrase" >< a name = "optional.introduction.solution" ></ a ></ span >< a class = "link" href = "index.html#optional.introduction.solution" > Solution</ a >
</ h4 >
2014-06-03 17:36:18 +02:00
< p >
2014-06-06 00:53:15 +02:00
This is how you solve it with < code class = "computeroutput" >< span class = "identifier" > boost</ span >< span class = "special" > ::</ span >< span class = "identifier" > optional</ span ></ code > :
</ p >
2014-06-03 17:36:18 +02:00
< pre class = "programlisting" >< span class = "preprocessor" > #include</ span > < span class = "special" > < </ span >< span class = "identifier" > boost</ span >< span class = "special" > /</ span >< span class = "identifier" > optional</ span >< span class = "special" > .</ span >< span class = "identifier" > hpp</ span >< span class = "special" > > </ span >
2014-05-07 17:07:12 +02:00
2014-06-03 17:36:18 +02:00
< span class = "identifier" > boost</ span >< span class = "special" > ::</ span >< span class = "identifier" > optional</ span >< span class = "special" > < </ span >< span class = "keyword" > int</ span >< span class = "special" > > </ span > < span class = "identifier" > getConfigParam</ span >< span class = "special" > (</ span >< span class = "identifier" > std</ span >< span class = "special" > ::</ span >< span class = "identifier" > string</ span > < span class = "identifier" > name</ span >< span class = "special" > );</ span > < span class = "comment" > // return either an int or a `not-an-int`</ span >
< span class = "keyword" > int</ span > < span class = "identifier" > main</ span >< span class = "special" > ()</ span >
< span class = "special" > {</ span >
< span class = "keyword" > if</ span > < span class = "special" > (</ span >< span class = "identifier" > boost</ span >< span class = "special" > ::</ span >< span class = "identifier" > optional</ span >< span class = "special" > < </ span >< span class = "keyword" > int</ span >< span class = "special" > > </ span > < span class = "identifier" > oi</ span > < span class = "special" > =</ span > < span class = "identifier" > getConfigParam</ span >< span class = "special" > (</ span >< span class = "string" > "MaxValue"</ span >< span class = "special" > ))</ span > < span class = "comment" > // did I get a real int?</ span >
< span class = "identifier" > runWithMax</ span >< span class = "special" > (*</ span >< span class = "identifier" > oi</ span >< span class = "special" > );</ span > < span class = "comment" > // use my int</ span >
< span class = "keyword" > else</ span >
< span class = "identifier" > runWithNoMax</ span >< span class = "special" > ();</ span >
< span class = "special" > }</ span >
2007-05-29 06:40:25 +00:00
</ pre >
</ div >
2014-06-06 00:53:15 +02:00
</ div >
2007-05-29 06:40:25 +00:00
< table xmlns:rev = "http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width = "100%" >< tr >
2021-11-20 02:00:30 +01:00
< td align = "left" >< p >< small > Last revised: November 20, 2021 at 00:57:52 GMT</ small ></ p ></ td >
2008-02-14 17:43:52 +00:00
< td align = "right" >< div class = "copyright-footer" ></ div ></ td >
2007-05-29 06:40:25 +00:00
</ tr ></ table >
< hr >
2014-06-04 18:13:06 +02:00
< div class = "spirit-nav" >< a accesskey = "n" href = "boost_optional/quick_start.html" >< img src = "../../../../doc/src/images/next.png" alt = "Next" ></ a ></ div >
2007-05-29 06:40:25 +00:00
</ body >
</ html >