![]() |
Home | Libraries | People | FAQ | More |
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); }