This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Possible range based 'for' bug


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]