Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: why use static_cast ?
Date: Tue, 15 Nov 2022 17:10:09 -0800
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <86v8nfd7ri.fsf@linuxsc.com>
References: <035d8c85-23d2-451b-a387-855b369b4c12n@googlegroups.com> <87k04u1lq4.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader01.eternal-september.org; posting-host="c33488f0e6c0635625a64620cc1d5893"; logging-data="2296957"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19eBaGiCdIsDd2GS2WCI+mPQFI6nnxQagk="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:1VSJcmOGxBKk4MUAEM+vdd37lQA= sha1:SIxDlxLGbIGqgV2Vq+d3fGA1Tjw=
Xref: csiph.com comp.lang.c++:87408
Keith Thompson writes:
> Andrey Tarasevich writes:
>
>> On 10/20/2022 4:23 AM, Paul N wrote:
>>
>>> On Thursday, October 20, 2022 at 6:21:25 AM UTC+1, Lynn McGuire wrote:
>>>
>>>> They are both pointers to 8 byte objects. I am just declaring
>>>> that I want to use the same address as a 8 byte character string
>>>> (no null !) or a long long integer.
>>>
>>> Isn't that exactly what a union is for? Though if you are hoping
>>> to put in values of one type and read them out as values of the
>>> other then you would need to check that this is actually defined
>>> in your implementation.
>>
>> "Exactly what a union is for"?
>>
>> It is true that unions have been used for type punning from time to
>> time, but this is not "what a union is for". Union is a
>> memory-time-sharing feature that exists for reducing memory usage.
>> You don't normally "put in values of one type and read them out as
>> values of the other". That's not exactly what union is for.
>
> Agreed.
>
> K&R1 (1978) introduces unions in section 6.8. It discusses holding
> objects of different types at different times; it says nothing
> about type punning. (The version of C discussed in the 1975 manual
> didn't have unions.)
Even so, clearly it was expected that unions would be used for
type punning, and that type punning would work the same way it
works now, even during the early days of C between K&R1 and when
the original ANSI C committee was formed. Evidence for this
assertion may be found in the C Rationale document, which says
nothing about using unions for type punning. The lack of any
mention in the Rationale document means it must have been common
practice in the early days, otherwise it would have been new
behavior and surely would have been discussed and debated and
written up in the Rationale document. Because it wasn't, unions
obviously were being used that way all along.