C++17

Shachar Shemesh via Digitalmars-d digitalmars-d at puremagic.com
Wed Jan 27 06:51:02 PST 2016


On 27/01/16 16:24, Minas Mina wrote:
> On Wednesday, 27 January 2016 at 14:22:18 UTC, Shachar Shemesh wrote:
>> On 26/01/16 11:33, deadalnix wrote:
>>> On Tuesday, 26 January 2016 at 09:16:47 UTC, Ola Fosheim Grøstad wrote:
>>>> [...]
>>> Now if one want to use that, D is very capable of doing it already. Just
>>> won't make it the default (like it is not the default in C++ either).
>>
>> I bring it up every time the subject comes up, in the hopes that at
>> some point it will sink in.
>>
>> No, D is not capable of doing it already. Without 100% reliable
>> destructors, RAII is simply not implementable.
>>
>> D's destructors are not guaranteed to run on 100% of fully initialized
>> structs, which means that a RAII container has no way to make sure its
>> resource is actually freed. It is up to the implementer. This
>> eradicates almost all of the utility RAII was meant to provide.
>>
>> Shachar
>
> Can you show a case where this happens?

Sure. Here it is, again:

import std.stdio;

struct RAII {
     int num;

     ~this() {
         writefln("Destructed %s", num);
     }
}

struct Container {
     int num;
     RAII raii;

     this(int num) {
         writefln("Constructing %s", num);
         this.num = num;
         raii = RAII(num);

         if( num==0 )
             throw new Exception("Ooops");
     }
}

void test(int num) {
     try {
         auto c = Container(num);
         writefln("Point %s", num);
     } catch(Exception ex) {
         writefln("Caught %s", ex.msg);
     }
}

void main()
{
     test(1);
     test(0);
}



More information about the Digitalmars-d mailing list