forked from boostorg/optional
Compare commits
9 Commits
boost-1.56
...
boost-1.57
Author | SHA1 | Date | |
---|---|---|---|
4c06d708d6 | |||
35d5e25672 | |||
a913650322 | |||
55dc4c1dde | |||
bda2001935 | |||
eef3bfe079 | |||
b7e8b1b54a | |||
d4a4cdca1d | |||
df01e9e429 |
@ -54,7 +54,7 @@ Distributed under the Boost Software License, Version 1.0.
|
||||
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.
|
||||
|
||||
[heading Problem]
|
||||
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.
|
||||
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 jut use `-1` to represent the absence of the parameter in the config file.
|
||||
|
||||
[heading Solution]
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
||||
|
||||
[section Optional return values]
|
||||
|
||||
Let's write and use a converter function that converts an a `std::string` to an `int`. It is possible that for a given string (e.g. `"cat"`) there exist no value of type `int` capable of representing the conversion result. We do not consider such situation an error. We expect that the converter can be used only to check if the conversion is possible. A natural signature for this function can be:
|
||||
Let's write and use a converter function that converts a `std::string` to an `int`. It is possible that for a given string (e.g. `"cat"`) there exists no value of type `int` capable of representing the conversion result. We do not consider such situation an error. We expect that the converter can be used only to check if the conversion is possible. A natural signature for this function can be:
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
boost::optional<int> convert(const std::string& text);
|
||||
@ -91,7 +91,7 @@ We could write function `convert` in a slightly different manner, so that it has
|
||||
return ans;
|
||||
}
|
||||
|
||||
The default constructor of `optional` creates an unitialized optional object. Unlike with `int`s you cannot have an `optional<int>` in an indeterminate state. Its state is always well defined. Instruction `ans = i` initializes the optional object. It uses the assignment from `int`. In general, for `optional<T>`, when an assignment from `T` is invoked, it can do two things. If the optional object is not initialized our case here), it initializes it with `T`'s copy constructor. If the optional object is already initialized, it assigns the new value to it using `T`'s copy assignment.
|
||||
The default constructor of `optional` creates an unitialized optional object. Unlike with `int`s you cannot have an `optional<int>` in an indeterminate state. Its state is always well defined. Instruction `ans = i` initializes the optional object. It uses the 'mixed' assignment from `int`. In general, for `optional<T>`, when an assignment from `T` is invoked, it can do two things. If the optional object is not initialized (our case here), it initializes the contained value using `T`'s copy constructor. If the optional object is already initialized, it assigns the new value to it using `T`'s copy assignment.
|
||||
[endsect]
|
||||
|
||||
[section Optional data members]
|
||||
|
@ -9,7 +9,7 @@ Type `optional<T>` is __SGI_EQUALITY_COMPARABLE__ whenever `T` is __SGI_EQUALITY
|
||||
|
||||
assert(oN != o0);
|
||||
assert(o1 != oN);
|
||||
assert(o2 != o1);
|
||||
assert(o0 != o1);
|
||||
assert(oN == oN);
|
||||
assert(o0 == o0);
|
||||
|
||||
@ -17,7 +17,7 @@ The converting constructor from `T` as well as from `boost::none` implies the ex
|
||||
|
||||
assert(oN != 0);
|
||||
assert(o1 != boost::none);
|
||||
assert(o2 != 1);
|
||||
assert(o0 != 1);
|
||||
assert(oN == boost::none);
|
||||
assert(o0 == 0);
|
||||
|
||||
|
@ -44,10 +44,10 @@
|
||||
return values</a>
|
||||
</h3></div></div></div>
|
||||
<p>
|
||||
Let's write and use a converter function that converts an a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>
|
||||
Let's write and use a converter function that converts a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>
|
||||
to an <code class="computeroutput"><span class="keyword">int</span></code>. It is possible that
|
||||
for a given string (e.g. <code class="computeroutput"><span class="string">"cat"</span></code>)
|
||||
there exist no value of type <code class="computeroutput"><span class="keyword">int</span></code>
|
||||
there exists no value of type <code class="computeroutput"><span class="keyword">int</span></code>
|
||||
capable of representing the conversion result. We do not consider such situation
|
||||
an error. We expect that the converter can be used only to check if the conversion
|
||||
is possible. A natural signature for this function can be:
|
||||
|
@ -49,11 +49,11 @@
|
||||
in an indeterminate state. Its state is always well defined. Instruction
|
||||
<code class="computeroutput"><span class="identifier">ans</span> <span class="special">=</span>
|
||||
<span class="identifier">i</span></code> initializes the optional object.
|
||||
It uses the assignment from <code class="computeroutput"><span class="keyword">int</span></code>.
|
||||
It uses the 'mixed' assignment from <code class="computeroutput"><span class="keyword">int</span></code>.
|
||||
In general, for <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code>,
|
||||
when an assignment from <code class="computeroutput"><span class="identifier">T</span></code>
|
||||
is invoked, it can do two things. If the optional object is not initialized
|
||||
our case here), it initializes it with <code class="computeroutput"><span class="identifier">T</span></code>'s
|
||||
(our case here), it initializes the contained value using <code class="computeroutput"><span class="identifier">T</span></code>'s
|
||||
copy constructor. If the optional object is already initialized, it assigns
|
||||
the new value to it using <code class="computeroutput"><span class="identifier">T</span></code>'s
|
||||
copy assignment.
|
||||
|
@ -41,7 +41,7 @@
|
||||
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">oN</span> <span class="special">!=</span> <span class="identifier">o0</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">o1</span> <span class="special">!=</span> <span class="identifier">oN</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">o2</span> <span class="special">!=</span> <span class="identifier">o1</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">o0</span> <span class="special">!=</span> <span class="identifier">o1</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">oN</span> <span class="special">==</span> <span class="identifier">oN</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">o0</span> <span class="special">==</span> <span class="identifier">o0</span><span class="special">);</span>
|
||||
</pre>
|
||||
@ -55,7 +55,7 @@
|
||||
</p>
|
||||
<pre class="programlisting"><span class="identifier">assert</span><span class="special">(</span><span class="identifier">oN</span> <span class="special">!=</span> <span class="number">0</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">o1</span> <span class="special">!=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">none</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">o2</span> <span class="special">!=</span> <span class="number">1</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">o0</span> <span class="special">!=</span> <span class="number">1</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">oN</span> <span class="special">==</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">none</span><span class="special">);</span>
|
||||
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">o0</span> <span class="special">==</span> <span class="number">0</span><span class="special">);</span>
|
||||
</pre>
|
||||
|
@ -107,7 +107,7 @@
|
||||
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
|
||||
to behave slightly differently. 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>
|
||||
@ -133,7 +133,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||
<td align="left"><p><small>Last revised: July 10, 2014 at 11:41:49 GMT</small></p></td>
|
||||
<td align="left"><p><small>Last revised: September 12, 2014 at 09:54:26 GMT</small></p></td>
|
||||
<td align="right"><div class="copyright-footer"></div></td>
|
||||
</tr></table>
|
||||
<hr>
|
||||
|
@ -13,6 +13,9 @@
|
||||
#define BOOST_BAD_OPTIONAL_ACCESS_22MAY2014_HPP
|
||||
|
||||
#include <stdexcept>
|
||||
#if __cplusplus < 201103L
|
||||
#include <string> // to make converting-ctor std::string(char const*) visible
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
|
||||
|
15
meta/libraries.json
Normal file
15
meta/libraries.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"key": "optional",
|
||||
"name": "Optional",
|
||||
"authors": [
|
||||
"Fernando Cacciola"
|
||||
],
|
||||
"description": "Discriminated-union wrapper for optional values.",
|
||||
"category": [
|
||||
"Miscellaneous"
|
||||
],
|
||||
"maintainers": [
|
||||
"Fernando Cacciola <fernando_cacciola -at- ciudad.com.ar>",
|
||||
"Andrzej Krzemienski <akrzemi1 -at- gmail.com>"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user