ref struct?

Steven Schveighoffer schveiguy at yahoo.com
Wed Oct 12 08:35:08 PDT 2011


On Sun, 09 Oct 2011 13:52:47 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> (I show this here because it's probably a silly idea, but it may a  
> chance to learn something.)
> Do you like the idea of a POD that is always managed by reference, as  
> class instances?
>
>
> ref struct Foo {}
> static assert(Foo.sizeof == 1);
> void main() {
>     Foo f1; // void reference
>     Foo f2 = new Foo; // by reference
> }
>
>
> It is as light as a struct, but you don't need to use the pointer syntax  
> to manage a Foo instance, the code is cleaner. There is no info field  
> inside a ref struct, so in some situations the destructor doesn't get  
> called, like regular structs.

You can achieve this with pImpl structs.  I think the only difference is  
the creation/destruction must be done via functions instead of new/GC.free.

-Steve


More information about the Digitalmars-d-learn mailing list