--- Anitha Boyapati <anithab@xxxxxxxxxxx> wrote: > > > On Fri, 21 Dec 2007, Ted Byers wrote: > > > > > First, I don't see the point of reinterpret cast > here, > > or even use of a void pointer. Unless this is a > > really dumbed down example, you probably ought to > > think about the types you'll be using for this. > > It is really a badly tailored version of a bigger > problem. > Actually, it all boils down to usage of types. With > "Value", I need to > able to represent both C++ data types (int, long, > long long) as well as > some of the user defined data types. Previously, I > used a union for built > in types and objects of user defined types packed > into Value. Now that > gave me a performance hit.(Everytime a Value is > initialised, all the > constructors of user defined types have to be gone > through unnecessarily > while an instance of Value reprensents only one of > the types) It was > really slow. So came a void pointer and a > reinterpret cast along with it. > Yuck! Why didn't you look at templates? If it makes sense to use multiple types in a given context, they MUST have common behaviours on which you rely. That is a starting point for generic programming. You can verify that the type for which the template is instantiated supports the behaviours you require using concept checking, with some template metaprogramming. But for your performance concerns, with a template version of your code, only the constructor for the type you require will be executed. And yout template version will probably be simpler and easier to read and maintain. An approach relying on void pointers and reinterpret casts strikes me as problematic at best, and something I'd rewrite at the first opportunity. It reminds me of a style of programming common before C++ was well developed and compilers provided decent support for templates. Not a great practice, but you make do with what is readily available to you at the time you're doing it. Since generic programming is now well supported, I'd suggest you make good use of it. Cheers, Ted