What is the difference between D and C++ regarding Unique, RefCounted and Scoped?

ponce via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 9 13:05:03 PDT 2015


On Wednesday, 9 September 2015 at 19:53:55 UTC, ponce wrote:
Oops, posted by mistake.

> On Wednesday, 9 September 2015 at 19:48:00 UTC, cym13 wrote:
>> Hi,
>>
>> I know C++ and D without being a C++ or D guru (I know way 
>> more about D though). When talking about memory management the 
>> problem of RAII is often mentioned along with the fact that 
>> classes use the GC. I know well the difference between structs 
>> and classes and don't want to talk about the GC here.
>>
>> It seems to me that just as one can manage his memory in C++ 
>> using unique_ptr, shared_ptr and basic RAII we can manage our 
>> memory using Unique, RefCounted and Scoped.
>>
>> My question is: what is possible in C++ that isn't in D?
>

C++ only has a D struct equivalent so all destructors are called 
deterministically. It's the addition of classes that create the 
problems in D.

C++ can also throw by value, something that D can't really do.

C++ objects can be:
- heap-allocated or not
- have deterministic destructors or not
- be polymorphic or not
without much restrictions.

If you find a way to have the equivalent of virtual functions and 
dynamic casts for structs, then all our problems are virtually 
solved and struct would be all we need.

>> How come that we are getting memory debates at all on this 
>> matter if we can do the same thing?

Because D has class objects as an addition, and people want to 
use them because they need both polymorphism and holding 
resources. This is a very common scenario.

Not all objects need a destructor, but when one need to have a 
destructor called, this propagates the need for clean-up to its 
owner too.

Hence, class objects that need deterministic destruction are very 
easy to come by in D programs.

>> If there are differences is fixing them an option to have 
>> something to show to D detractors?

There was a proposal to stop the GC from calling destructors, 
which didn't take.
There was also proposals of RC classes.



More information about the Digitalmars-d-learn mailing list