What is going on here?
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Fri Mar 6 21:09:31 PST 2015
On 3/4/15 2:31 PM, Shachar Shemesh wrote:
> On 04/03/15 17:43, Steven Schveighoffer wrote:
>> On 3/4/15 8:43 AM, Shachar Shemesh wrote:
>>
>>> I'd expect A's destructor to run, which does not seem to be the case.
>>
>>
>> I believe destructors are not run when you throw inside a constructor.
>> So plan to deallocate if the ctor throws:
>>
>> a = A(var + 1);
>> scope(failure) destroy(a);
>>
>> -Steve
>
> That's pretty horrible. So now I need to keep track of the
> implementation of my members to figure out whether it is okay to skip
> the destructor or not.
Why does it matter? If the member is constructed, insert a
scope(failure) destroy, otherwise don't. Or don't throw in the ctor and
mark it nothrow, then you don't have to worry about it at all.
> All of this from a language that claims to support RAII, which was
> invented precisely so the programmer does not have to keep track of
> those things.
It would be nice if there is a way. D, unlike C++, does not separate
construction of members from the code. This means the situation is not
as easy to solve.
But an interesting question -- when constructing a struct or class,
isn't every member initialized with it's init value anyway? Why would
calling dtor on that be bad?
-Steve
More information about the Digitalmars-d
mailing list