This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Possible range based 'for' bug
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Julian Klappenbach <jklappenbach at gmail dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Sun, 21 Jun 2015 19:29:16 +0100
- Subject: Re: Possible range based 'for' bug
- Authentication-results: sourceware.org; auth=none
- References: <CAJ49vaUdYycmCPFEhCmJ9uSAzj2pCdp8oRC=yx1NSG1yZgNExA at mail dot gmail dot com> <CAJ49vaVYqrGCxh46riYjiZea-9vXNzBfTHqwUPi6NPsvzQ-Ftg at mail dot gmail dot com> <CAH6eHdSiONCF8LCtuasaQhGmW8Drq5fDZ6sDgYUVNWCnFi4xMw at mail dot gmail dot com> <CAJ49vaXMLKfB=hhbSXjMOzvBS-qi4+X8fXa6ehbJ-n=PooxvkA at mail dot gmail dot com>
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
http://en.cppreference.com/w/cpp/language/range-for
And this is still off-topic on this list.