|
Boost : |
From: Zara (yozara_at_[hidden])
Date: 2007-10-11 07:55:58
On Thu, 11 Oct 2007 13:43:47 +0200, Roland Schwarz
<roland.schwarz_at_[hidden]> wrote:
>
>
>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
>
I may not work for PODs. It depends on the data alignment of the
compiler, and the data alignment capabilities of the processor.
You are invoking the dreaded devils of Undefined Behaviour!
Bes regards,
Zara
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk