|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2002-11-24 08:29:29
Pavol Droba <droba_at_[hidden]> writes:
> Hi,
>
> I have developed a simple cast function which I found very useful. Here it is:
>
> template< typename T >
> inline T offset_cast( void* p, unsigned int offset=0 )
> {
> return reinterpret_cast< T >( static_cast<unsigned char*>( p )+offset );
> }
>
> template< typename T >
> inline T offset_cast( const void* p, unsigned int offset=0 )
> {
> return reinterpret_cast< T >( static_cast<const unsigned char*>( p )+offset );
> }
>
> Its purposes is to simplify a mapping of C-like structure onto binary data. Here is
> the example: Assume thath you have a network packet in raw format, and you want
> to read IP-header information. You could do following:
>
> ip_header* ip=offset_cast<ip_header>( packet, ip_header_offset );
>
> instead of ugly pointer arithmetic.
>
> Is there any interest for something like this in Boost?
My answer: "not if it uses reinterpret_cast, since that's not
portable".
-- David Abrahams dave_at_[hidden] * http://www.boost-consulting.com Boost support, enhancements, training, and commercial distribution
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk