From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2006-02-04 13:51:33


Wang Weiwei wrote:
> template <typename destination_type, typename source_type>
> destination_type force_cast(source_type s)
> {
> union
> {
> source_type s;
> [...]

Looks like an almost failsafe recipe for disaster ;-).

It isn't safe to use 'union' in a generic context like this one. Union members should be of POD type (BTW. that's why there is Boost.Variant).

>
> ... a pointer to non-virtual function is actualy a normal function pointer:
>

Another dangerous assumption:

With MSVC's __thiscall calling convention (the default setting of this compiler) the this-pointer is passed in a CPU register (namely ECX) and not on the stack...

Sorry for all the bad news,

Tobias

P.S: An array_cast that allows to change array bounds and dimensions (with checking, in contexts where possible) would have my vote (that is, if it's not in Boost already, somewhere)...