boostorg / foreach

Boost.org foreach module
http://boost.org/libs/foreach
9 stars 36 forks source link

Add support for movable, non-copyable iterators. #1

Closed LEW21 closed 10 years ago

LEW21 commented 10 years ago

This way it's possible to use BOOST_FOREACH with move-only iterators.

ericniebler commented 10 years ago

Iterators are required to be CopyConstuctible and CopyAssignable. If coroutine's iterators are not, then they are not iterators.

LEW21 commented 10 years ago

But... C++11 range-based for works on them perfectly. And BOOST_FOREACH is also able to do so - with this trivial patch. There are no technical reasons to require copying here.

ericniebler commented 10 years ago

All the more reason not to change boost_foreach: just use C++11 range-based for. Boost_foreach is legacy code for C++03 users. It's been frozen for years. It's used everywhere. I am extremely reluctant to make any changes that has the potential to break code on all the ancient compilers it's been tested with and frequently used on.

I would much rather see coroutine's iterators be made to conform to the standard concept requirement for iterators.

LEW21 commented 10 years ago

Coroutine's iterators are currently copyable, but..

When copied, they still refer to the same coroutine. And when you do:

auto it2 = it;
++it;
++it2;

it is not equal to it2. it2 is the element after it.

Probably nothing in the standard says that this behavior is illegal, but it's quite unexpected, and breaking common assumptions about iterators.

And it's impossible to support copying correctly, as we would need to copy the coroutine's stack.

ericniebler commented 10 years ago

You are describing an InputIterator. Another good example of an InputIterator is std::istream_iterator. The standard is very specific about the syntax and semantics the different refinements of the iterator concepts, and which algorithms work with which refinements. Please read it.