How to use structs for RAII?

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


On 2011-01-23 01:14, Andrej Mitrovic wrote:
> A workaround:
>
> import std.stdio;
> import std.exception;
>
> struct A
> {
>      int x;
>
>      this(void* none)
>      {
>          if (none !is null)
>          {
>              enforce(0, "Tried to pass a parameter to A's constructor");
>          }
>
>          writeln("in constructor");
>          // construction from here..
>          x = 5;
>      }
> }
>
> void main()
> {
>      auto a = A(null);
> }
>
> I think that would be safe, and closest to a "default" constructor.

A static opCall would be better. See my answer to the original post.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list