Boost logo

Boost :

Subject: Re: [boost] [Range] New I/O functions for ranges
From: Indiana (indi.in.the.wired_at_[hidden])
Date: 2014-10-05 11:46:43


On 14-10-01 05:00 AM, Paul A. Bristow wrote:
> You may well be right, but I'm only reporting what I found were my 'needs'.
>
> You can show how to wrap write_all output (with 'smart' delimiter - no ugly
> trailing commas!) with prefix and suffix strings, that will probably be fine.
Yeah, sure - actually write_all() does that right out of the box:

o << "prefix" << write_all({1,2,3}, "delimiter") << "suffix";
// Prints: "prefix1delimiter2delimiter3suffix";

So:

o << '{' << write_all({1,2,3}, ',') << '}';
// Prints: "{1,2,3}"
> I only meant a string in C++ syntax like
>
> int[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
>
> that you have shown is possible.
Actually, you can probably generalize that. The following code isn't
tested, but:

template <typename Range, typename CharT, typename Traits>
void format_range(std::basic_ostream<CharT, Traits>& o, Range&& r)
{
   using std::begin;
   using std::end;
   using value_type = std::decay_t<typename range_value<Range>::type>;

   std::basic_ostringstream<CharT, Traits> oss{};
   oss.imbue(std::locale::classic());

   oss << boost::typeindex::type_id<value_type>().pretty_name();
   oss << '[' << std::distance(begin(r), end(r)) << ']' << " = ";
   oss << '{' << write_all(std::forward<Range>(r), ", ") << "};\n";

   o << oss.str();
}

Calling format_range() for any range type should give the output you
want, more or less, dependent on the range value type.

auto v = std::vector<int>{1,2,3};
cout << format_range(v); // Prints: "int[3] = {1, 2, 3};"
auto const a = std::array<double, 4>{1.1, 2.2, 3.3, 4.4};
cout << format_range(a); // Prints: "double[4] = {1.1, 2.2, 3.3, 4.4};"
> Now you need some enthusiastic users to support you on a review ;-)
Yeah? I'm not sure how the process goes here. Do I make a pull request
on the Boost.Range repository and implement the changes? Or do I
implement the idea separately, so people can experiment and mess around
with it, and only after it's passed review try to integrate it with the
rest of the library? Do I get in touch with the Boost.Range maintainers
(seems to be Thorsten Ottosen and Neil Groves)?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk