Boost logo

Boost :

Subject: Re: [boost] [Range] New I/O functions for ranges
From: Indiana ([hidden])
Date: 2014-09-27 22:51:16

On 14-09-27 09:24 PM, Edward Diener wrote:
> Why not "write" instead of "write_all" ?

I'm not great with coming up with names, but I figured write() is not
descriptive enough. The key reason I thought "write" alone would not do
is because strings are ranges, and writing a string to an output stream
means something very different than writing all (or each) of the
characters in that string in turn:

// BEGIN CODE /////////////////////////
auto str = std::string{"abc"};

// What do you expect on reading:
cout << write(str, 'x');
// versus
cout << write_all(str, 'x');

// Personally, for the former I expect "abcx", not "axbxc".
// END CODE ///////////////////////////

The names I considered were things like "write_range()",
"print_range()", "stream_range()", "write_each()", "out_all()",
"all_of()", "each_of()", and so on. Basically, I wanted to drive the
point home that each element in the range is being written, in turn and
in order, without being too wordy.

If anyone wants to suggest better names for any of the functions, go for it.

> How do I "write_all" a range to some other i/o output type( files,
> strings etc. ) ?

It's the same no matter what you're reading/writing from:

// BEGIN CODE /////////////////////////
auto const r = vector<double>{1.0, 2.0, 3.0};

// to the console:
cout << write_all(r, ',');

// to a file:
ofstream outfile{"file.out"};
outfile << write_all(r, ',');

// to a socket:
// assume io_service, endpoint, acceptor, and error_code are set up
boost::asio::ip::tcp::ostream tcpos;
acceptor.accept(*tcpos.rdbuf(), error_code);
tcpos << write_all(r, ',');

// to a string:
ostringstream oss{};
oss << write_all(r, ',');

// even to a wide-character stream:
wcout << write_all(r, ',');
// (works because all streams support op<< for char and char*;
// wouldn't work if the delimiter were a std::string - it would
// need to be a std::wstring in this case)
// END CODE ///////////////////////////

And the same is true for all the input functions and any input stream type.

Boost list run by bdawes at, gregod at, cpdaniel at, john at