[Joke] C++ and D namings

Steven Schveighoffer schveiguy at gmail.com
Wed Jan 20 14:33:11 UTC 2021


On 1/20/21 12:12 AM, Ola Fosheim Grostad wrote:
> On Wednesday, 20 January 2021 at 05:04:44 UTC, Steven Schveighoffer wrote:
>> On 1/19/21 11:21 PM, Ola Fosheim Grostad wrote:
>> This is undefined behavior in D. How does it lead to different code 
>> Gen? This code could pass in D, but the equivalent C++ would have to 
>> fail since it's not UB:
>>
>> int i = 3;
>> const int *j = &i;
>> *(cast(int *)j) = 4;
>> assert(i == 3);
> 
> Wasnt D supposed to eliminate UB?

No.

> 
> How about this?
> 
> union t {int x; const int y;};
> 
> 

What's wrong with that? const is allowed to refer to mutable data.

But it's not usable in @safe code (which IS supposed to eliminate UB).

But technically, you can set one union member and as long as you don't 
use the other, it's never UB. In this case, however, it would seem fine.

Your immutable case would ALWAYS be UB, but only if you accessed the 
member you didn't set.

And again, disallowed in @safe code.

-Steve


More information about the Digitalmars-d mailing list