forked from boostorg/optional
83 lines
6.5 KiB
HTML
83 lines
6.5 KiB
HTML
![]() |
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
||
|
<title>Introduction</title>
|
||
|
<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
|
||
|
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
|
||
|
<link rel="home" href="../index.html" title="Chapter 1. Boost.Optional">
|
||
|
<link rel="up" href="../index.html" title="Chapter 1. Boost.Optional">
|
||
|
<link rel="prev" href="../index.html" title="Chapter 1. Boost.Optional">
|
||
|
<link rel="next" href="../boost_optional/quick_start.html" title="Quick Start">
|
||
|
</head>
|
||
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||
|
<table cellpadding="2" width="100%"><tr>
|
||
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
|
||
|
<td align="center"><a href="../../../../../index.html">Home</a></td>
|
||
|
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
|
||
|
<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>
|
||
|
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
|
||
|
</tr></table>
|
||
|
<hr>
|
||
|
<div class="spirit-nav">
|
||
|
<a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../boost_optional/quick_start.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
|
||
|
</div>
|
||
|
<div class="section">
|
||
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||
|
<a name="optional.introduction"></a><a class="link" href="introduction.html" title="Introduction">Introduction</a>
|
||
|
</h2></div></div></div>
|
||
|
<p>
|
||
|
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="introduction.html#optional.introduction.problem">Problem</a>
|
||
|
</h4>
|
||
|
<p>
|
||
|
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
|
||
|
to behave slightly different. Also suppose that any possible value of type
|
||
|
<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 jut 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.
|
||
|
</p>
|
||
|
<h4>
|
||
|
<a name="optional.introduction.h1"></a>
|
||
|
<span class="phrase"><a name="optional.introduction.solution"></a></span><a class="link" href="introduction.html#optional.introduction.solution">Solution</a>
|
||
|
</h4>
|
||
|
<p>
|
||
|
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>
|
||
|
<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>
|
||
|
|
||
|
<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>
|
||
|
</pre>
|
||
|
</div>
|
||
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||
|
<td align="left"></td>
|
||
|
<td align="right"><div class="copyright-footer">Copyright © 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright © 2014 Andrzej Krzemieński<p>
|
||
|
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></td>
|
||
|
</tr></table>
|
||
|
<hr>
|
||
|
<div class="spirit-nav">
|
||
|
<a accesskey="p" href="../index.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../boost_optional/quick_start.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|