libstdc++/10783: std::vector::reverse_iterator could be smaller

Nathan Myers ncm@cantrip.org
Mon May 19 03:08:00 GMT 2003


On Sun, May 18, 2003 at 09:35:50PM +0200, Paolo Carlini wrote:
> Well, on second thought, and giving justice to the clear
> explanation in V&J, in their ?16.2.2 it is clearly stated
> that the EBCO has no equivalent for data members: this is
> reasonable considering that it would create problems with
> the representation of pointers to members.
> 
> Therefore reverse_iterator is expected to have the same
> size of its iterator empty base (thanks to EBCO) + the size
> of its member current, that is two times the size of a
> plain iterator.
> 
> http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=10783

I agree it would be a Good Thing for the reverse iterators to 
be the same size as the regular iterators.

It seems to me that this is one of those cases where the regular
empty-base optimization can't be applied.  That is, the ABI
forbids it because two base-class subobjects of the same type
would have the same type.

Often you can get around this sort of thing by giving the base an 
extra, defaulted, dummy argument, and deriving from a variant,
so the two base subobjects that share the same address have 
different types.  I think that doesn't work here because the 
derivation hierarchy and the argument list to std::iterator<> might 
be fixed by the standard.

We might be able to get around it by giving each container a private 
iterator type, and then deriving the public iterator type from that, 
mixing in std::iterator<>.  Then the reverse iterator would (be 
specialized to just contain an instance of the base type, and also 
mix in std::iterator<>.

Another would be simply to derive privately from the regular iterator 
type and override some members.  It would be nice to make a template
that does this, so it could be used for all the containers.

A tricky way would be to arrange that the addresses of the two base
subobjects are at opposite ends of the object:

  struct empty {};
  struct notempty { int i; };

  struct iterator : empty { notempty n; };   // sizeof is 4

  struct riterator_base { iterator i; };  // has empty at offset 0
  struct riterator // has empty at both offsets 0 and 4.
    : riterator_base, empty {};   // sizeof should still be 4.

Unfortunately this doesn't work.  sizeof(riterator) is 8. :-(
This is probably a result of an unfortunate oversight by the
ia64 ABI group.

Nathan Myers
ncm-nospam@cantrip.org



More information about the Gcc-bugs mailing list