C++'s this() equivalent?

Steven Schveighoffer schveiguy at gmail.com
Fri Jun 16 00:52:24 UTC 2023


On 6/15/23 8:45 PM, zjh wrote:
> On Friday, 16 June 2023 at 00:35:48 UTC, Steven Schveighoffer wrote:
>> But there is no way in D to have e.g.:
>>
>> ```d
>> B b; // runs a constructor
>> ```
>>
> 
> 
> As a `C++` user, it is very terrible to simplify `RAII` without such a 
> method. Why must `this()` be disabled? Can't there be a constructor? In 
> a constructor, you can complete `some work`. Without it, how can you 
> simplify `RAII`?.
> 

D was not originally designed with RAII. That was added later (D1 
structs did not have ctors or dtors).

The benefit of *not* having parameterless constructors is that default 
construction is always defined *and* available at compile time.

What is wrong with using a parameter constructor or factory function? 
What is the feature you are trying to build that you can with C++, but 
not with D?

I'm not saying I agree with the limitation, just want to know more.

-Steve


More information about the Digitalmars-d-learn mailing list