| Home | Libraries | People | FAQ | More

#### I/O

The I/O operators: `<<` and `>>` work generically on all Fusion sequences. The I/O operators are overloaded in namespace `boost::fusion` [9]

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.

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 >> set_open('[') >> set_close(']') >> set_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.

###### Header

    #include <boost/fusion/sequence/io.hpp>
    #include <boost/fusion/include/io.hpp>

[9] __sequences__ and Views residing in different namespaces will have to either provide their own I/O operators (possibly forwarding to fusion's I/O operators) or hoist fusion's I/O operators (using declaration), in their own namespaces for proper argument dependent lookup.