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";
}
}