Language features and reinterpret casts
Jonathan M Davis
jmdavisProg at gmx.com
Mon Sep 20 18:30:18 PDT 2010
On Monday, September 20, 2010 18:21:53 Jesse Phillips wrote:
> bearophile Wrote:
> > - C Standard says that assigning to one member of a union and then
> > accessing a different member is undefined behaviour. - GCC is a
> > practical compiler, so it has the -fno-strict-aliasing switch, that
> > allows to use that trick. See "-Wstrict-aliasing" and:
> > http://stackoverflow.com/questions/2906365/gcc-strict-aliasing-and-casti
> > ng-through-a-union - Walter said that regarding such union semantics D
> > acts as C. So that union trick will break with other future D compilers.
>
> I believe that defined behavior within an area where C is specified to be
> undefined is completely in line with operating as C does. But where does
> Walter say Unions act as C in this case? Why not, define the behavior and
> leave it.
Well, since C doesn't define the behavior, D can do anything - including using
behavior that it defines and considers entirely consistent. So, as long as it's D
code, I see no reason why D can't use the same constructs as C code but have
things which are undefined in C be defined in D. The only problem is if you port
code from D to C, but that's not exactly something that we're generally worried
about. Overall though, I get the impression that Walter's goal is to get as
close to having no undefined behavior in D as he reasonably can. Whether the
behavior is defined in another language or not isn't particularly relevant.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list