2017-04-06 13:38 GMT+02:00 Norbert Pfeiler
<[email protected]>:
> 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.

True.  The reason why we prefer such patter is that it works in both
languages as desired.  Otherwise we would be in need to write
different variants for the languages C/C++.  This looks backward, and
it still doesn't make sure that there might be warnings emitted for C,
or C++ doing const casts.

Cheers,
Kai


> Kai Tietz <[email protected]> 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 <[email protected]>:
>> >> 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
>> > [email protected]
>> > 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
>> [email protected]
>> 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
> [email protected]
> 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
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to