Boost logo

Boost Users :

Subject: Re: [Boost-users] Needs advices on design ( mpl or processor )
From: Allan Nielsen (a_at_[hidden])
Date: 2012-01-10 15:08:47


> IIUC, the EnumUnion has no tag, which means you could store an Enum1
> and retrieve an Enum2 without any warning.  Is that correct?
> In UnionModel, there's:
>    static const size_t size
>      = head::size > _tail::size ? head::size : _tail::size;

Almost, the EnumUnion has a tag, but this is only used to find the
specific union. Consider:


> double_union;

Here I have two union, where three enums shares the same storage.

> which makes sense because a union just has to be large enough to store
> the largest enumeration.  However, in the case of StructModel_r, I
> don't understand:
>    static const size_t size = head::size * _tail::size;
> around line 200.  I would think that there would just be additions
> since you want to store one value after another.  That's why, in my
> previous post, in template sum_bits, there's:
>      , integral_c<unsigned,Bits+enum_bits<Enum>::size>
> Could you explain why multiplication instead of addition is used to
> calculate the size in StructModel_r?

This is because the offset is not really an offset, I just could not
come up with a better name. Here is the explanation:

Assume we need to store 5 different enumerated types which each
represents 3 different values ( like the A, B C).

These 5 enums can all together represent 3^5 = 243 different values (

As 243 is less than 256 ( all the different values in a char), it is
possible to store the values in a char. But in a binary number system
2 bits are required to store one tri-state value. If we use 3 * 5 bits
we end up with 15 bits ( more than one char) which is not the most
efficient encoding.

Therefore we encode this in a base_3 number system which is defined
by: ... d_2 * n^2 + d_1 * n^1 + d_0 * n^0, where d is the digits we
want to encode, and n is 3 because all the enums are tri-states.

Example: Encode BCACB -> 12021

1*3^4 + 2*3^3 + 0*3^2 + 2*3^1 + 1*3^0 = 142

To extract enum number 4 the reverse operation must be done: (142 / 3^3) % 3 = 2

If different enums are with different sizes are used then n in the
equation above will be different for each digit. Then to set digit
number 4 one has to multiply n_0 * n_1 * n_2 * n_3 and then use this
as base. Therefore the multiplications.

> Could you explain why multiplication instead of addition is used to
> calculate the size in StructModel_r?
Currently no, but I would like to add this. Also a range check on the
array access.

Best regards
Allan W. Nielsen

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at