How to use structs for RAII?

Jacob Carlborg doob at me.com
Sun Jan 23 06:43:29 PST 2011


On 2011-01-23 00:03, Sean Eskapp wrote:
> It was recommended to me to use structs for RAII instead of scope classes,
> since scope is being removed (?). However, since default-constructors for
> structs can't exist, how does one do this?

You can use a static opCall, like this:

struct Foo
{
     static Foo opCall ()
     {
         Foo foo;
         // initialize foo
         return foo;
     }
}

auto foo = Foo();

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list