digitalmars.D.learn - D has the same memory model as C++
- Tejas (7/7) Aug 10 2021 What exactly does the above statement mean?
- Paul Backus (14/18) Aug 10 2021 No. D and C++ have different semantics for many things. One
- Tejas (24/44) Aug 10 2021 Yes, I should've been more precise.
- Bastiaan Veelo (3/5) Aug 10 2021 You *can* cast away `const` in D: https://run.dlang.io/is/sWa5Mf
- Tejas (3/8) Aug 10 2021 Yes, but it is UB, not defined and supported by the
- Bastiaan Veelo (5/17) Aug 10 2021 OK, strictly speaking casting away `const` is in the language,
- Bastiaan Veelo (4/6) Aug 10 2021 The only gotcha that comes to my mind is that `private` means
- Tejas (14/20) Aug 10 2021 A few others that I know:
- Bastiaan Veelo (14/36) Aug 11 2021 How is that different from C++? I think you meant to say that if
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/4) Aug 11 2021 This is an interesting thread but "memory model" does not cover or mean
- Tejas (3/8) Aug 11 2021 A little bit of spoon feeding isn't bad, right?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (6/7) Aug 11 2021 I DuckDuckGo'ed it for you. :)
- Tejas (8/15) Aug 11 2021 I was hoping on material regarding. c++ vs D memory model, not
- Paul Backus (17/38) Aug 11 2021 D's memory model is described on this page of the language spec:
What exactly does the above statement mean? Also, I have read here a principle that **If it looks like C, it behaves like C** How true is that for C++? Does code that look like C++(minus obvious syntax differences) behave like C++? Does having the same memory model help? Thanks for reading!!
Aug 10 2021
On Tuesday, 10 August 2021 at 11:05:53 UTC, Tejas wrote:Also, I have read here a principle that **If it looks like C, it behaves like C** How true is that for C++? Does code that look like C++(minus obvious syntax differences) behave like C++?No. D and C++ have different semantics for many things. One example is `const`. In C++, the following code is totally fine: ```c++ int n = 123; // mutable object const int *p = &n; // const pointer const_cast<int*>(p) = 456; // ok to mutate via p ``` However, the equivalent code in D is undefined behavior: ```d int n = 123; // mutable object const(int)* p = &n; // const pointer cast(int*)p = 456; // not allowed to mutate via p ```
Aug 10 2021
On Tuesday, 10 August 2021 at 13:18:24 UTC, Paul Backus wrote:On Tuesday, 10 August 2021 at 11:05:53 UTC, Tejas wrote:Yes, I should've been more precise. I was hoping for an answer in terms of implicit conversions, default constructors that get created by the respective compilers(ignoring the move constructors), sequence points(Walter said that the sequence point rules are same for D and C, so is it safe to assume they're the same as C++ as well?), also whether there is any difference in template instantiations(IFTI, or template argument detection rules) Also, about the const thing, since there is no ```const_cast``` in D, therefore it is not the same as D, yet doesn't violate the statement that C++ that looks like D behaves the same as D, since that would require you to write ```(int*)``` but there's casting away const, a clearly seperate language feature which has no equivalent in D; it's kind of like saying that ``` int a[] = [1,2,3]``` is not the same as in D since in D that's a dynamic array, not static. Yeah it isn't but there is an equivalent representation for it available, unlike ```const_cast``` Sorry for the ambiguous statement below Basically, what are the subtle gotcha's in the differences between C++ and D code that looks similar (again, I fully understanding I'm ambiguous here, but I simply don't know how to express this otherwise) Thank you for replying!Also, I have read here a principle that **If it looks like C, it behaves like C** How true is that for C++? Does code that look like C++(minus obvious syntax differences) behave like C++?No. D and C++ have different semantics for many things. One example is `const`. In C++, the following code is totally fine: ```c++ int n = 123; // mutable object const int *p = &n; // const pointer const_cast<int*>(p) = 456; // ok to mutate via p ``` However, the equivalent code in D is undefined behavior: ```d int n = 123; // mutable object const(int)* p = &n; // const pointer cast(int*)p = 456; // not allowed to mutate via p ```
Aug 10 2021
On Tuesday, 10 August 2021 at 16:00:37 UTC, Tejas wrote:there's casting away const, a clearly seperate language feature which has no equivalent in D;You *can* cast away `const` in D: https://run.dlang.io/is/sWa5Mf — Bastiaan.
Aug 10 2021
On Tuesday, 10 August 2021 at 18:07:35 UTC, Bastiaan Veelo wrote:On Tuesday, 10 August 2021 at 16:00:37 UTC, Tejas wrote:Yes, but it is UB, not defined and supported by the standard/implementation.there's casting away const, a clearly seperate language feature which has no equivalent in D;You *can* cast away `const` in D: https://run.dlang.io/is/sWa5Mf — Bastiaan.
Aug 10 2021
On Tuesday, 10 August 2021 at 18:13:17 UTC, Tejas wrote:On Tuesday, 10 August 2021 at 18:07:35 UTC, Bastiaan Veelo wrote:OK, strictly speaking casting away `const` is in the language, but modifying after that is UB. https://dlang.org/spec/const3.html#removing_with_cast — Bastiaan.On Tuesday, 10 August 2021 at 16:00:37 UTC, Tejas wrote:Yes, but it is UB, not defined and supported by the standard/implementation.there's casting away const, a clearly seperate language feature which has no equivalent in D;You *can* cast away `const` in D: https://run.dlang.io/is/sWa5Mf — Bastiaan.
Aug 10 2021
On Tuesday, 10 August 2021 at 16:00:37 UTC, Tejas wrote:Basically, what are the subtle gotcha's in the differences between C++ and D code that looks similarThe only gotcha that comes to my mind is that `private` means private to the module in D, not private to the aggregate. — Bastiaan.
Aug 10 2021
On Tuesday, 10 August 2021 at 21:19:39 UTC, Bastiaan Veelo wrote:On Tuesday, 10 August 2021 at 16:00:37 UTC, Tejas wrote:A few others that I know: When importing one module(say ```m2```) into another (say ```m1```), the symbols of ```m1``` aren't automatically visible in ```m2```. You must import ```m1``` in ```m2``` for that. You can't just initialize variables willy-nilly in a module, you must do them inside ```static this()``` Method function pointers don't exist in D, equivalent functionality is achieved by using a delegate that captures the context of a class instantiated object. To use a function pointer explicitly, you can't just write ```(func)(arguments)```, it is ```(&func)(arguments)```(thank you evilrat) ```sizeof/alignof``` differs in D and C++(thank you evilrat)Basically, what are the subtle gotcha's in the differences between C++ and D code that looks similarThe only gotcha that comes to my mind is that `private` means private to the module in D, not private to the aggregate. — Bastiaan.
Aug 10 2021
On Wednesday, 11 August 2021 at 05:33:06 UTC, Tejas wrote:On Tuesday, 10 August 2021 at 21:19:39 UTC, Bastiaan Veelo wrote:How is that different from C++? I think you meant to say that if `m1` imports `m2`, the symbols of `m2` don't automatically become visible by importing `m1`. Indeed you also have to import `m2` for that, *unless* `m1` `public`ly imports `m2`.On Tuesday, 10 August 2021 at 16:00:37 UTC, Tejas wrote:A few others that I know: When importing one module(say ```m2```) into another (say ```m1```), the symbols of ```m1``` aren't automatically visible in ```m2```. You must import ```m1``` in ```m2``` for that.Basically, what are the subtle gotcha's in the differences between C++ and D code that looks similarThe only gotcha that comes to my mind is that `private` means private to the module in D, not private to the aggregate. — Bastiaan.You can't just initialize variables willy-nilly in a module, you must do them inside ```static this()```Or declare them with an initial value. I'm not aware of a rule that requires module level variables to be initialized in `static this()`, but if you require them to be initialized at startup with a value not known at compile time, then that is the place to do it. I don't think you can go about this willy-nilly in C++ either...Method function pointers don't exist in D, equivalent functionality is achieved by using a delegate that captures the context of a class instantiated object. To use a function pointer explicitly, you can't just write ```(func)(arguments)```, it is ```(&func)(arguments)```(thank you evilrat)To take the address of a method (in order to assign it to a delegate) requires `&`, yes, but not when calling the delegate. --Bastiaan.
Aug 11 2021
This is an interesting thread but "memory model" does not cover or mean all of the points discussed here. I can't define it precisely, so I'm leaving it to interested parties to search for themselves. :) Ali
Aug 11 2021
On Wednesday, 11 August 2021 at 19:04:50 UTC, Ali Çehreli wrote:This is an interesting thread but "memory model" does not cover or mean all of the points discussed here. I can't define it precisely, so I'm leaving it to interested parties to search for themselves. :) AliA little bit of spoon feeding isn't bad, right? Atleast leave some pointers on where to start :(
Aug 11 2021
On 8/11/21 12:19 PM, Tejas wrote:Atleast leave some pointers on where to start :(I DuckDuckGo'ed it for you. :) https://en.cppreference.com/w/cpp/language/memory_model Then looked it up at Wikipedia too: https://en.wikipedia.org/wiki/Memory_model_(programming) Ali
Aug 11 2021
On Wednesday, 11 August 2021 at 19:27:34 UTC, Ali Çehreli wrote:On 8/11/21 12:19 PM, Tejas wrote:I was hoping on material regarding. c++ vs D memory model, not C++ model alone. That's the problem. For pure C++, there's even the paper by Stroustroup et al. which feels pretty definitive. That's why I asked here whether anyone could tell me about the subtle differences between C++ and D, with regards to memory model, or any other features that share surface resemblance, but are different underneath.Atleast leave some pointers on where to start :(I DuckDuckGo'ed it for you. :) https://en.cppreference.com/w/cpp/language/memory_model Then looked it up at Wikipedia too: https://en.wikipedia.org/wiki/Memory_model_(programming) Ali
Aug 11 2021
On Wednesday, 11 August 2021 at 19:38:38 UTC, Tejas wrote:On Wednesday, 11 August 2021 at 19:27:34 UTC, Ali Çehreli wrote:D's memory model is described on this page of the language spec: https://dlang.org/spec/intro.html I'm not super familiar with C++'s memory model, but just from eyeballing the cppreference page, it looks like the main difference between the C++ and D memory models is how they handle access to memory from multiple threads. In particular, D makes a distinction between "thread-local memory locations", which may only be accessed from one thread, and "shared memory locations", which may be accessed from multiple threads. In C++, any memory location can be accessed from any thread. The other big difference is that D's spec does not really define its terms. What exactly it means for a thread to "access" a memory location, or for concurrent accesses to be "synchronized", is never actually explained. For these details, it is *probably* safe to fill in the blanks with the equivalent definitions from C++.On 8/11/21 12:19 PM, Tejas wrote:I was hoping on material regarding. c++ vs D memory model, not C++ model alone. That's the problem. For pure C++, there's even the paper by Stroustroup et al. which feels pretty definitive. That's why I asked here whether anyone could tell me about the subtle differences between C++ and D, with regards to memory model, or any other features that share surface resemblance, but are different underneath.Atleast leave some pointers on where to start :(I DuckDuckGo'ed it for you. :) https://en.cppreference.com/w/cpp/language/memory_model Then looked it up at Wikipedia too: https://en.wikipedia.org/wiki/Memory_model_(programming) Ali
Aug 11 2021