RAII
Arun Chandrasekaran via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Feb 23 23:06:29 PST 2017
On Thursday, 23 February 2017 at 09:57:09 UTC, ketmar wrote:
> Arun Chandrasekaran wrote:
>
>> I'm trying to write an RAII wrapper on Linux.
>>
>> I understand struct in D doesn't have default constructor (for
>> .init reasons).
>> I don't want to use `scope`.
>>
>> Is there an elegant way to achieve this in D?
> why not static method or free function that returns struct? due
> to NRVO[0] it won't even be copied.
>
> auto lock = MyWrapper();
>
> `MyWrapper()` may return voldemort type, so user won't create
> your struct accidentally.
>
>
> [0] https://dlang.org/glossary.html#nrvo
Thanks for your help. NRVO looks interesting. However this may
not be RAII after all. Or may be too much of C++ has spoiled me.
I am not familiar enough with D to appreciate/question the
language design choice. I'll accept this and move forward and see
how my code evolves. Nevertheless, I'm still learning something
new. :)
More information about the Digitalmars-d-learn
mailing list