blob: 0fb4e894fb68d5cccecccc172780919520edda7c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
[section IO operators]
It is possible to use `optional<T>` with IO streams, provided that `T` can be used with streams. IOStream operators are defined in a separate header.
``
#include <iostream>
#include <boost/optional/optional_io.hpp>
int main()
{
boost::optional<int> o1 = 1, oN = boost::none;
std::cout << o1;
std::cin >> oN;
}
``
The current implementation does not guarantee any particular output. What it guarantees is that if streaming out and then back in `T` gives the same value, then streaming out and then back in `optional<T>` will also give back the same result:
``
#include <cassert>
#include <sstream>
#include <boost/optional/optional_io.hpp>
int main()
{
boost::optional<int> o1 = 1, oN = boost::none;
boost::optional<int> x1, x2;
std::stringstream s;
s << o1 << oN;
s >> x1 >> x2;
assert (o1 == x1);
assert (oN == x2);
}
``
[endsect]
|