D has the same memory model as C++

Paul Backus snarwin at gmail.com
Tue Aug 10 13:18:24 UTC 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
```


More information about the Digitalmars-d-learn mailing list