Boost logo

Boost :

From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2007-10-11 09:25:26

Zara wrote:
> yes, certainly. Just check if the endiannes is the right one, or if
> you must caculate the toal the oother way round.

Thank you.

I have to add:
My example posted was bogus. I just tried to compile and never
instantiated the class. When I did I saw that the compiler complained
anyways. To make it work I had to use:

template<class T>
struct packed
     unsigned char data[sizeof(T)];
     operator T&()
     { return *reinterpret_cast<T*>(data); }

And then I looked into the standard:
5.2.10 clause 7:
A pointer to an object can be explicitly converted to an object of
different type.

I think this is my case.
But of course:
.., the result of such a pointer conversion is unspecified.

And this I guess is what you meant.

So I stick with the latter, explicit code.

So the question is left:

unsigned char mem[] = {1,2,3,4,5};

struct foo
     packed<char> a;
     packed<int> b;

How do I map my struct to mem avoiding surprise:

foo& f((foo&)(*mem));

I guess this is bad again, as for the very same reasons of unspecified
behaviour, true? At least I cannot use a static_cast instead of (foo&) cast.

Thanks again

Roland aka speedsnail

Boost list run by bdawes at, gregod at, cpdaniel at, john at