D has the same memory model as C++

Tejas notrealemail at gmail.com
Tue Aug 10 16:00:37 UTC 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:
>> 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
> ```

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!



More information about the Digitalmars-d-learn mailing list