Possible range based 'for' bug

Jonathan Wakely jwakely.gcc@gmail.com
Sun Jun 21 18:29:00 GMT 2015

On 21 June 2015 at 19:16, Julian Klappenbach wrote:
> The issue occurred to me after I sent the email.
> begin() / end() return iterators by value, not reference.
> So, you're correct in identifying the value / reference issue.  But to
> be precise:  you can't return an abstract class type *by value* from a
> function.

I was already being precise when I said you can't return an abstract
class type from a function. If you return by reference you are not
returning a class type, you are returning a reference type :-)

>  If the return values of begin() / end() were returned and
> accepted as a reference, then I believe this would work.  This
> difference is that return by value results in an implicit
> instantiation, which would attempt to create the
> abstract type.
> Given that the existing libs all are coded to return iterators by
> value from begin() / end(), I would assume that converting the
> range-for logic to work with references would cause some nasty
> side-effects.

That is not an option, the standard is very explicit about the
behaviour of range-based for, and whatever begin() returns is copied
by value, so still wouldn't work with abstract classes even if you
changed your begin() to return a reference. See

And this is still off-topic on this list.

More information about the Gcc mailing list