On terça-feira, 5 de novembro de 2013 12:24:32, Marc Mutz wrote:
> On Tuesday, November 05, 2013 01:07:32 Thiago Macieira wrote:
> > -            return (void *)qptrdiff(data);
> > +            char *null = 0;
> > +            return null + qptrdiff(data);
> 
> Since this is equivalent to
> 
>     return 0[qptrdiff(data)];
> 
> isn't that dereferencing the nullptr and therefore undefined behaviour?

No. This is equivalent to:

        return &0[qptrdiff(data)];

There's no dereferencing.

Adding/subtracting null is a good way to make pointer-to-integer conversions 
without casts.

> What's wrong with
> 
>    reinterpret_cast<void*>(qptrdiff(data));

Nothing, but it's uglier since it has a cast.
 
> Or simply
> 
>   return ptr;
> 
> which forms a union with `data`?

Now that's undefined behaviour under C++98 (accessing a union member that was 
not the last one you set).

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
Development mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to