digitalmars.D.learn - What is the difference between D and C++ regarding Unique, RefCounted
- cym13 (13/13) Sep 09 2015 Hi,
- ponce (3/16) Sep 09 2015 C++ only h
- ponce (25/43) Sep 09 2015 C++ only has a D struct equivalent so all destructors are called
- cym13 (12/39) Sep 09 2015 This is subtly missing the main question: isn't C++-like memory
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (3/6) Sep 09 2015 I don't think it is a good idea to tell C++ programmers that they
- cym13 (4/11) Sep 09 2015 Hence my question: in what? (I assume you are talking about
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (8/10) Sep 09 2015 I think you should just refer them to the relevant man pages.
- ponce (7/10) Sep 09 2015 - Unique
- Gary Willoughby (3/5) Sep 10 2015 RefCounted works with classes as well.
- Daniel =?UTF-8?B?S296w6Fr?= (7/15) Sep 10 2015 struct RefCounted(T, RefCountedAutoInitialize autoInit =
- Gary Willoughby (3/19) Sep 10 2015 Sorry my mistake.
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
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 Howcome 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
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: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.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?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.How come that we are getting memory debates at all on this matter if we can do the same thing?There was a proposal to stop the GC from calling destructors, which didn't take. There was also proposals of RC classes.If there are differences is fixing them an option to have something to show to D detractors?
Sep 09 2015
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.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)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.How come that we are getting memory debates at all on this matter if we can do the same thing?There was a proposal to stop the GC from calling destructors, which didn't take. There was also proposals of RC classes.If there are differences is fixing them an option to have something to show to D detractors?
Sep 09 2015
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 itI 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
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:Hence my question: in what? (I assume you are talking about Unique RefCounted etc and not about 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 itI 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
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
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
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.
Sep 10 2015
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: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- RefCounted Only for D structs. std::shared_ptr works for all.RefCounted works with classes as well.
Sep 10 2015
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:Sorry my mistake.On Wednesday, 9 September 2015 at 23:22:49 UTC, ponce wrote: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- RefCounted Only for D structs. std::shared_ptr works for all.RefCounted works with classes as well.
Sep 10 2015