# C++ Range based stream

Hoping somebody has knowledge of the range based additions going into the standard. So they can point out all the things I need to add to make it compliant.

### IterableStream.h

#include <iostream>
#include <iterator>

namespace ThorsAnvil::Util
{

template<typename T>
class IterableStream
{
std::istream&      stream;
public:
IterableStream(std::istream& stream)
: stream(stream)
{}
operator std::istream&()            {return stream;}

std::istream_iterator<T> begin()    {return std::istream_iterator<T>(stream);}
std::istream_iterator<T> end()      {return std::istream_iterator<T>();}
};

}


Example usage

#include "IterableStream.h"
#include <iostream>
#include <fstream>

namespace TaU = ThorsAnvil::Util;
int main()
{
std::ifstream x("plop");

for(auto const& y: TaU::IterableStream<int>(x)) {
std::cout << y << "\n";
}
}


I don't like the name - it ought to be more explicit that it's an input-only range.

Otherwise, it looks just as a range should, with the observation that we omit the usual const iterators, as they don't make sense here.

I think you can avoid repetition of the return type:

std::istream_iterator<T> begin()    {return {stream};}
std::istream_iterator<T> end()      {return {};}


First of all, I would change the name to istream_range:

template<typename ValueType>
class istream_range


This will make it more in line with snake_case which standard uses, and is both cool sounding and descriptive. May be ValueType is a bit overkill, but I think that it makes it more clear clear that template parameter is for values.

I think that range should be copyable. Input iterators are copyable too, but people know that it gets invalidated. It is usefull when chaining/pipelining/propagating. The only addition would be making stream a pointer and putting some dereferences.

I'm not sure which is the latest paper, but here is the quote from n4128:

We’ve already decided that Ranges (not Iterables) are copyable and assignable

The paper notes that there is an Iterable, but the title of the post says range.

My opinion is that ranges should make user be able to do everything iterators can, but in a simpler and less verbose way. It would be core concept to keep in mind.

std::istringstream would be better for example. It is more convenient, and gives reviewers more control.