Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: Union type punning in C++
Date: Tue, 21 Jan 2020 03:47:38 -0800
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <86eevtjajp.fsf@linuxsc.com>
References: <868sme2err.fsf@linuxsc.com> <86v9pfguqn.fsf@linuxsc.com> <86muaqgjt6.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="d01dc2e4789bccc98e4eaed72337c14b"; logging-data="32204"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181oqszTa8cUweFn1uwd20rZU75SgrNjpQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:ZqnlfKBrEjoazy8PuLSmreygqYY= sha1:E2vCumuonwRx6Tn/ny131PSESYs=
Xref: csiph.com comp.lang.c++:71483
"Chris M. Thomasson" writes:
> On 1/14/2020 7:05 AM, Tim Rentsch wrote:
>
>> "Chris M. Thomasson" writes:
>>
>> [...]
>>
>>> I added in the extraneous base in the union out of some sort of
>>> reflex mixed with trying to just read from o.b.
>>
>> Maybe you were thinking of programming in C.
>
> I am a C programmer at heart. [example omitted]
>> In C there is often benefit to have a "stub" type that serves
>> mainly to differentiate the "concrete" types. AFAICT in C++
>> there is not, except perhaps as some sort of aid to the reader.
I should elaborate on this. In C, if there is a union with several
structs as members, it's okay to access a member in the common
initial sequence of a struct actually in a union object, __given
only a pointer to the struct__ (assuming the full definition of the
union is visible). In C++, I believe the "common initial sequence"
rule applies only when using the union type as part of the access
expression. The C++ rule is a significantly weaker guarantee than
the C rule. To me it seems to rob the feature of much of its
value, even not counting the incongruity with the C rule. At this
point there are so many differences between C and C++ that it seems
inappropriate to say C is a subset of C++, even just mostly.
> Interesting points to ponder. Actually, a decent chunk of my C++
> work in the past was wrapping up a C API. A lot of times the C API
> would be wrapping up some externally assembled, assembly language.
>
> For some reason, sometimes, when I created a pure C++ object, I
> would code it up in C on my free time. Well, this was a habit wrt
> interfaces that need to be exported in a shared lib. A nice clean C
> API is great for this. C++ can have some issues wrt creating, say
> .so, or .dlls.
>
> A C binding is okay with me.
Be warned: the different treatments, by C and by C++, of the
union "special guarantee", might mean that calling through a C
binding may result in undefined behavior in C++, even if the
C code is well-defined by the C standard. It isn't clear to me
what the implications of C++'s rules are in this area, and in
particular whether a C++ implementation may be allowed to make
some assumptions that would not be allowed under the C rules.
I'm sorry I don't have any better advice to offer other than
it would be wise to tread cautiously.