Generic functions to convert to void* and from void*

Daniel Keep daniel.keep.lists at gmail.com
Mon Feb 23 17:28:25 PST 2009


TSalm wrote:
>>>> I'm trying to build function which have the hability to convert a type
>>>> to void* and from void*.
>>
>> First of all, I have to ask: have you looked at std.variant /
>> tango.core.Variant?
>>
> Yes, but it seems that Variant class uses more memory than void* .

The Phobos Variant will use however much space you reserve as the
maximum, plus 4 bytes for a function pointer, but it can only store
types as big as you allow for.  The Tango version will use
max(real.sizeof,void[].sizeof) + 4 bytes for the typeid and can store
anything you throw at it.

For that extra space, both of these will give you runtime type safety,
meaning you can't accidentally get the types wrong.  They're MUCH safer
than void*.

>>>> [...]
> 
>> I get the distinct impression that you're seriously over-thinking this.
>>  Both of these functions could be rewritten as casts.  Aside from that,
>> you've given no context for me to have any idea what you're trying to
>> accomplish here.
>>
> 
> I'm really a newbie concerning the use of void* ( I think you have
> notice this ;-)  )
> Thanks for your usefull remarks.

void* is just a pointer like any other.  It doesn't have any special
properties except that you cannot dereference it; that's it.  If you're
not sure how to use pointers, then don't.

For example, you could store objects instead; this takes the same amount
of storage in the DBMS, and allows for safe casting back to the original
type.  Plus, you don't have to stuff about with casting things to void*
and back.

> I'm simply trying to make a little and lightweight "DBMS in memory".
> Simply made by classes like TableMem, RowMem and ColumnMem(T).
> There's also a "private" class which aims to store datas, using void*[][].

Unless you really need to store small value types like integers, etc. in
that private data, objects might be the best bet for now.

  -- Daniel


More information about the Digitalmars-d-learn mailing list