lundi 31 octobre 2016

Why `std::istream_iterator` has not a rvalue constructor?

There is any specific reason that std::istream_iterator cannot receive the stream as an rvalue?

For passing a temporary object, I have to create a function like:

template<class T, class ostream_t>
std::istream_iterator<T> my_it(ostream_t&& ostream)
{ return {ostream}; }

template<class T>
std::istream_iterator<T> my_it() { return {}; }

int main()
{
    std::string file("3.45 1.23 7,56");

    std::copy(my_it<double>(std::istringstream(file)), my_it<double>(),
              std::ostream_iterator(out, " "));
}

However, that would be more convenient, and shorter:

int main()
{
    std::string file("3.45 1.23 7,56");

    using my_it = std::istream_iterator<double>;

    std::copy(my_it(std::istringstream(file)), my_it(),
              std::ostream_iterator(out, " "));
}

Why, after three standard updates (C++11, C++14 and C++17), there is no rvalue constructor for that kind of iterators, when nearly every any other type have it?

You can argue that, since the iterator is copyable and holds a reference, you can get undefined behaviour if the referenced object is no longer alive (std::reference_wrapper has the rvalue construct disabled as well), but that can also happen with lvalue references.

It's an user land responsability after all.

Aucun commentaire:

Enregistrer un commentaire