...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The I/O operators: <<
and >>
work generically
on all Fusion sequences. The I/O operators are overloaded in namespace
boost::fusion
[10]
The operator<<
has been overloaded for generic output streams such that Sequence(s)
are output by recursively calling operator<<
for each element. Analogously,
the global operator>>
has been overloaded to extract Sequence(s)
from generic input streams by recursively calling operator>>
for each element.
Please note that, to display your adapted types via fusion IO system, corresponding overloaded operators should be introduced to same namespace of the type.
namespace your_awesome_library { using boost::fusion::operators::operator>>; // for input using boost::fusion::operators::operator<<; // for output ...
The default delimiter between the elements is space, and the Sequence is enclosed in parenthesis. For Example:
vector
<float, int, std::string> a(1.0f, 2, std::string("Howdy folks!");
cout << a;
outputs the vector
as: (1.0 2 Howdy folks!)
The library defines three manipulators for changing the default behavior:
Manipulators
tuple_open(arg)
Defines the character that is output before the first element.
tuple_close(arg)
Defines the character that is output after the last element.
tuple_delimiter(arg)
Defines the delimiter character between elements.
The argument to tuple_open
,
tuple_close
and tuple_delimiter
may be a char
, wchar_t
,
a C-string, or a wide C-string.
Example:
std::cout << tuple_open('[') << tuple_close(']') << tuple_delimiter(", ") << a;
outputs the same vector
, a
as: [1.0, 2, Howdy folks!]
The same manipulators work with operator>>
and istream
as well. Suppose the std::cin
stream contains the following data:
(1 2 3) [4:5]
The code:
vector
<int, int, int> i;vector
<int, int> j; std::cin >> i; std::cin >> tuple_open('[') >> tuple_close(']') >> tuple_delimiter(':'); std::cin >> j;
reads the data into the vector
(s) i
and j
.
Note that extracting Sequence(s)
with std::string
or C-style string elements does
not generally work, since the streamed Sequence
representation may not be unambiguously parseable.
#include <boost/fusion/sequence/io.hpp> #include <boost/fusion/include/io.hpp>