Re: aggregate casting
On Dec 4, 5:59 pm, restor <akrze...@gmail.com> wrote:
We all know dynamic/static cast - to travel along hierarchy.
Is there a good way to cast along aggregates?
Hi, first, from the code, and form your last line comment, it looks
like you are doing something evil. ...
I can disagree with the 'evil' how come dynamic_cast is 'ok'? it
should be 'evil' in exact same manner.
but, yes i'm aware to the fact that C++ programmers have some common
habits/language.
one may also argue that dynamic casting is between well defined
objects, well so is the above aggregate cast.
....
template< typename From, typename To, From To::* Offset >
To* aggregate_cast1( From* ptr )
{
void * voidOffs = &( static_cast<To*>(NULL) ->* Offset );
char * toOffs = static_cast<char*>( voidOffs );
ptrdiff_t diff = toOffs - static_cast<char*>(NULL);
void * begin = ptr;
void * x = static_cast<char*>(begin) - diff;
return static_cast<To*>( x );
}
template< typename From, typename To >
To* aggregate_cast2( From* ptr, From To::* Offset )
{
void * voidOffs = &( static_cast<To*>(NULL) ->* Offset );
char * toOffs = static_cast<char*>( voidOffs );
ptrdiff_t diff = toOffs - static_cast<char*>(NULL);
void * begin = ptr;
void * x = static_cast<char*>(begin) - diff;
return static_cast<To*>( x );
}
They can be used like this:
A *a = aggregate_cast1< B, A, &A::b >( b );
A *a = aggregate_cast2( b, &A::b );
The above solutions are exactly what i need, and it helped me in
saving lots of memory...
However those solutions are evil and will only work under certain
conditions:
1. Struct A must be a POD: no virtual members, no virtual inheritance.
I disagree, i don;t think A must be a POD, actually i think your
solution is practically fine for all type of objects. I did check it
on a small example to verify.
2. I am not sure how it works with different data alignments.
Again, i think you did correct coding, no issues with alignments.
3. The only safe way to use thus converted pointer to A is to use its
member A::b.
Regards,
&rzej
10x
Rabin
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]