I’m pretty sure that cast via union is UB in C++ whereas C-style casting
away constness isn’t (only writing to the resulting object would be) but it
may result in compilers issuing warnings about style (because C++ has
const_cast).

Best, Norbert.

Kai Tietz <ktiet...@googlemail.com> schrieb am Do., 6. Apr. 2017 um
11:50 Uhr:

> A cast via union looks like this:
> type1 *foo(const type1 *my_const_ptr)
> {
> union {
>   type1 *t1;
>   const type1 *ct1;
> } v;
> v.ct1 = my_const_ptr;
> return v.t1;
> }
>
> The advantage of such a pattern is that no type conversion
> errors/warnings are shown.  So for casting from const to none-const,
> this variant is to be preferred.  (and it works for C, and C++!!!)
>
> Cheers,
> Kai
>
> 2017-04-05 15:51 GMT+02:00 Mateusz Mikuła <mati...@gmail.com>:
> >> Hmm, using here "wchar_t *" as cast looks wrong.  Actually we should
> >> use anyway PUWSTR_C instead.
> > I noticed it a bit too late and sent another patch casting to PUWSTR_C.
> >> Nevertheless we can have here a const/none-const conversion (means
> >> const specifiers for C-runtime function isn't regarded right?).  I
> >> would suggest to introduce a union-cast instead to avoid further
> >> warnings instead.
> > Conversion from const pointer to normal pointer is definitely unsafe but
> > that's probably what GCC just did.
> > I'm unsure what you mean by "union-cast" but you can commit your fix.
> >
> >
> >
> ------------------------------------------------------------------------------
> > Check out the vibrant tech community on one of the world's most
> > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> > _______________________________________________
> > Mingw-w64-public mailing list
> > Mingw-w64-public@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
> >
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Mingw-w64-public mailing list
> Mingw-w64-public@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to