Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2006-05-15 15:39:31


Larry Evans wrote:
> On 05/15/2006 07:49 AM, David Abrahams wrote:
>> Joel de Guzman <joel_at_[hidden]> writes:
>>
>>> David Abrahams wrote:
> [snip]
>>>> Seems to me a vector can't do as well as some other structures when
>>>> many of the elements of the tuple are empty classes.
>>> In terms of memory footprint?
>> Yes.
>>
> Isn't the list<T0,T1,T2,...> memory the same as that of:
>
> struct
> { T0 car;
> struct
> { T1 car;
> struct
> { T2 car;
> struct
> {
> ...
> } cdr;
> } cdr;
> } cdr;
> };
>
> and the vector<T0,T1,T2,...> memory the same as that of:
>
> struct
> { T0 m0;
> T1 m1;
> T2 m2;
> ...
> }
>
> ? If so, what's the reason the memory footprints would be different?
> If not, could you explain what is the footprint, preferably using
> some struct counterpart like those above?

Here's a sketch:

     template <class Head, class Tail>
     struct cons : compressed_pair<Head, Tail> { /*...*/ };

So, given empty elements such as:

     struct T0 {};
     struct T1 {};
     struct T2 {};

We'd get:

     struct cons
     {
        struct cons
        {
           struct cons
           {
           };
        };
     };

Also empty! See attached:

    size:1

Regards,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

#include <boost/compressed_pair.hpp>
#include <iostream>

template <class Head, class Tail = nil>
struct cons : boost::compressed_pair<Head, Tail>
{
};

struct T0 {};
struct T1 {};
struct T2 {};
struct nil {};

int main()
{
    std::cout << "size:" <<
        sizeof(cons<T0, cons<T1, cons<T0, nil> > >) << std::endl;
    return 0;
}


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