www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the difference between D and C++ regarding Unique, RefCounted

reply "cym13" <cpicard openmailbox.org> writes:
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? How come 
that we are getting memory debates at all on this matter if we 
can do the same thing? If there are differences is fixing them an 
option to have something to show to D detractors?
Sep 09 2015
parent reply "ponce" <contact gam3sfrommars.fr> writes:
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 h How
 come that we are getting memory debates at all on this matter 
 if we can do the same thing? If there are differences is fixing 
 them an option to have something to show to D detractors?
Sep 09 2015
parent reply "ponce" <contact gam3sfrommars.fr> writes:
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.
Sep 09 2015
parent reply "cym13" <cpicard openmailbox.org> writes:
On Wednesday, 9 September 2015 at 20:05:06 UTC, ponce wrote:
 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.
This is subtly missing the main question: isn't C++-like memory management of D classes possible with Unique, RefCounted and Scoped? I understand the limitations you mentioned, but it seems to me that we already have a way to use classes which deterministically calls the destructor and frees classes from most of the GC's limitations. If one wants them on the stack reserving memory and emplacing is easy too. How does that not answer rants from people wanting C++ back? (I won't hide that the other current thread on "class destructor" is at the origin of this one, but I think the topics are different enough to justify two threads)
Sep 09 2015
next sibling parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Wednesday, 9 September 2015 at 20:17:44 UTC, cym13 wrote:
 This is subtly missing the main question: isn't C++-like memory 
 management of D classes possible with Unique, RefCounted and 
 Scoped? I understand the limitations you mentioned, but it
I don't think it is a good idea to tell C++ programmers that they work the same, since they are quite different.
Sep 09 2015
parent reply "cym13" <cpicard openmailbox.org> writes:
On Wednesday, 9 September 2015 at 20:34:03 UTC, Ola Fosheim 
Grøstad wrote:
 On Wednesday, 9 September 2015 at 20:17:44 UTC, cym13 wrote:
 This is subtly missing the main question: isn't C++-like 
 memory management of D classes possible with Unique, 
 RefCounted and Scoped? I understand the limitations you 
 mentioned, but it
I don't think it is a good idea to tell C++ programmers that they work the same, since they are quite different.
Hence my question: in what? (I assume you are talking about Unique RefCounted etc and not about classes).
Sep 09 2015
parent "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Wednesday, 9 September 2015 at 20:37:40 UTC, cym13 wrote:
 Hence my question: in what? (I assume you are talking about 
 Unique RefCounted etc and not about classes).
I think you should just refer them to the relevant man pages. Explanations are just going to be confusing as they are different. You can compare for yourself: http://en.cppreference.com/w/cpp/memory http://en.cppreference.com/w/cpp/language/value_category http://dlang.org/phobos/std_typecons.html http://dlang.org/phobos/std_algorithm_mutation.html
Sep 09 2015
prev sibling parent reply "ponce" <contact gam3sfrommars.fr> writes:
On Wednesday, 9 September 2015 at 20:17:44 UTC, cym13 wrote:
 This is subtly missing the main question: isn't C++-like memory 
 management of D classes possible with Unique, RefCounted and 
 Scoped?
- Unique C++ has move semantics which make moves explicit. D's Unique is more like the deprecated C++'s auto_ptr: it has an opAssign overload that changes the owner. - RefCounted Only for D structs. std::shared_ptr works for all.
Sep 09 2015
parent reply "Gary Willoughby" <dev nomad.so> writes:
On Wednesday, 9 September 2015 at 23:22:49 UTC, ponce wrote:
 - RefCounted

 Only for D structs. std::shared_ptr works for all.
RefCounted works with classes as well. http://dlang.org/phobos/std_typecons.html#.RefCounted
Sep 10 2015
parent reply Daniel =?UTF-8?B?S296w6Fr?= <kozzi dlang.cz> writes:
On Thu, 10 Sep 2015 11:38:35 +0000
"Gary Willoughby" <dev nomad.so> wrote:

 On Wednesday, 9 September 2015 at 23:22:49 UTC, ponce wrote:
 - RefCounted

 Only for D structs. std::shared_ptr works for all.
RefCounted works with classes as well. http://dlang.org/phobos/std_typecons.html#.RefCounted
struct RefCounted(T, RefCountedAutoInitialize autoInit = RefCountedAutoInitialize.yes) if (!is(T == class) && !is(T == interface)); if (!is(T == class) && !is(T ==interface)); // So it does not work with classes
Sep 10 2015
parent "Gary Willoughby" <dev nomad.so> writes:
On Thursday, 10 September 2015 at 12:34:54 UTC, Daniel Kozák 
wrote:
 On Thu, 10 Sep 2015 11:38:35 +0000
 "Gary Willoughby" <dev nomad.so> wrote:

 On Wednesday, 9 September 2015 at 23:22:49 UTC, ponce wrote:
 - RefCounted

 Only for D structs. std::shared_ptr works for all.
RefCounted works with classes as well. http://dlang.org/phobos/std_typecons.html#.RefCounted
struct RefCounted(T, RefCountedAutoInitialize autoInit = RefCountedAutoInitialize.yes) if (!is(T == class) && !is(T == interface)); if (!is(T == class) && !is(T ==interface)); // So it does not work with classes
Sorry my mistake.
Sep 10 2015