Andrew Haley wrote:
Why not make it a union of Datatype* and void* ?
Then you wouldn't need to cast at all.
Datatype is a template argument that is known at the time the casts are
instantiated but can't be known nor even used as a template argument at
the time the pointer (that was void** and now is my_union*) is
instantiated, nor at the point that the copying is done.
1) Allocate (using malloc for some stupid reason) a my_union[] at a
point where Datatype can't be known.
2) Fill that array with nulls
3) In code where Datatype is a template argument, pass various
Datatype*& from within that array to several routines which overwrite
some of those nulls.
4) Steps 1 and 2 for a different my_union[] (again can't know Datatype)
5) In other code that can't know Datatype, copy some void*'s from the
first array to the second. The optimizer discarded this code before my
change.
6) In code where Datatype is a template argument, pass Datatype*const&
from within the second array to several routines which use those
Datatype*'s.
Under strict aliasing, the void*'s copied in step 5 are never used, and
everything that touches the second array across its entire lifetime is
inlined into one place, so the compiler knows those void*'s are never used.
The Datatype*'s that exist at the same address as those void*'s are used.
Datatype is never int. The union tells the compiler that an int* exists
at the same address as the void*. Even though the compiler inlines
enough code to see the int* is never used, somehow this defeats the
optimization.