This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: PR 57779 New debug check
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: François Dumont <frs dot dumont at gmail dot com>
- Cc: Paolo Carlini <paolo dot carlini at oracle dot com>, "libstdc++ at gcc dot gnu dot org" <libstdc++ at gcc dot gnu dot org>
- Date: Thu, 11 Jul 2013 21:49:48 +0100
- Subject: Re: PR 57779 New debug check
- References: <51DDC4A0 dot 6020008 at gmail dot com> <CAH6eHdS-X3B61M3+6NcYmiZ69Q80xda9JUMN7ciZgOC8=yZz8A at mail dot gmail dot com> <51DEE0A9 dot 9010809 at oracle dot com> <CAH6eHdQtkAqrDTsSQ5S7xE8P2ABK3JzOAuYNvAB7pxOGWprXSQ at mail dot gmail dot com> <51DF1291 dot 6040404 at gmail dot com>
On 11 July 2013 21:16, François Dumont wrote:
> I am indeed dereferencing the end iterator but as long as it is just to
> get the address of the resulting element it is fine.
I don't think that's what the standard says.
C99 and later say that &*p is OK if p is a null pointer, but C++ does
not have the same rule, see e.g.
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232 for an
open issue on this topic. Even if that proposed resolution were to be
accepted (very unlikely, it's sat untouched for many years) what
you're doing is not the same, because you dereference an iterator
which returns a reference, and _then_ you take the address, so it's
equivalent to:
T& end() { T* p = 0; return *p; }
T* p = &end();
That forms an invalid reference before taking the address, so is not
the same as &*(T*)0, it's more like &(T&)*(T*)0 and I'm uncomfortable
with that.