From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2007-10-11 07:43:47


Zara wrote:
> class foo {
> private:
> unsigned char internals[5];
> public:
> char a() {return internals[0];}
> int b() {
> // select code block depending on your endianness
> unsigned int n=(a[0]<<24)|(a[1]<<16]|(a[2]<<8)|a[3];
> unsigned int n=(a[3]<<24)|(a[2]<<16]|(a[1]<<8)|a[0];
> return static_cast<int>(n);
> }
> };

Hmm, I tried the following:

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

struct foobar
{
     packed<char> a;
     packed<int> b;
}

which seems to work by try.
But is this standard conformant usage?
(I need this only for POD's)

Roland aka speedsnail