struct to/from void, object to/from void

Era Scarecrow rtcvb32 at yahoo.com
Sun Apr 29 16:39:10 PDT 2012


On Sunday, 29 April 2012 at 22:35:19 UTC, Jason King wrote:
> I'm another of what seem to be legions of people trying to 
> interface with OS stores that keep void * in c/c++.  My 
> particular one is Windows TLSData, but for my example I don't 
> need any Windows code, just D.


>     void* opCast() {
>         return &this;
>     }

  Honestly, in my opinion, drop this. Don't make it more 
'pointer-like'. Unless there's a good reason, or it makes sense 
(Mathematical), then don't. You keep having half the tests using 
regular &(address) operator; what's wrong with that?

  Structs having no inheritance or virtual functions hold no extra 
information, which is one reason you can void cast them from one 
type or from void to their type without any issues. Casting to an 
object though I am not sure, but I think it won't let you unless 
it is able to. Besides once you have the pointer you don't really 
need to deference unless you want to copy the struct.

  I'm not sure how safe casting an object to void* would be, or 
back. If you can't avoid it, I would say make overloaded methods 
that do the intermediate work and make it transparent for you 
afterwards.


More information about the Digitalmars-d-learn mailing list