Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2000-06-30 14:24:07


jsiek_at_[hidden] wrote on 6/30/2000 3:03 PM
>I was a bit disgruntled last night when I found out that
>compressed_pair didn't work with g++ :(
>
>The problem is that g++ doesn't really do the empty base
>optimization.
>
>struct foo { };
>struct bar : public foo { int x; };
>struct yow : public foo { };
>
>sizeof(foo)= 1
>sizeof(bar)= 8 (should be 4)
>sizeof(yow)= 1

I didn't know this about g++ (I don't have it installed).

>This causes several problems. First boost::is_empty gives the wrong
>result for foo.

Given that the optimization is not performed, I would argue that is_empty
does indeed give the correct result on g++. :-\

>I tried the following workaround which seemed to work
>(though someone more familiar with this should make sure it is ok).
>
>template <typename T>
>struct empty_helper_t1 : public T
>{
>#ifdef __MWERKS__
> empty_helper_t1(); // hh compiler bug workaround
>#endif
>#ifndef __GNUC__
> // g++ only optimizes space when derived class is empty :(
> int i[256];
>#endif
>};
>struct empty_helper_t2 {
>#ifndef __GNUC__
> int i[256];
>#endif
>};

This may give a false positive for: struct foo {char c;};

>The second problem, of course is that the compressed_pair mechanism
>won't reduce the size as it should with g++. I then started to try to
>think of alternate ways to implement compressed_pair that would work
>with g++.
>
>One idea was to have a specialization of the compressed_pair's
>implementation class that is just plain missing the empty class object
>(instead of inheriting from it). The problem with this is that if the
>empty class does something in its constructor/destructor, then that
>something will not happen as it should.

And what would compressed_pair return when asked for that member?
Default construct it and return it by value? I suspect you may be better
off paying for the 4 bytes in size.

>Can anyone think of other alternatives or workarounds?

Sorry, compressed_pair was never intended to compress on those platforms
where the compiler optimization was not performed. However
compressed_pair _is_ designed to otherwise be functionally equivalent on
those platforms. So you just write your code once, and if the compiler
optimizes, then your code takes advantage of that. If not, then not.
<shrug>

-Howard


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk