public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Unions, common-initial subsequence and UB
@ 2022-10-20 11:46 Wilhelm Meier
  2022-10-20 13:10 ` Wilhelm Meier
  0 siblings, 1 reply; 2+ messages in thread
From: Wilhelm Meier @ 2022-10-20 11:46 UTC (permalink / raw)
  To: gcc-help

Hi all,

this is more a general question instead of a g++ specific one:

the standard explicitly makes this exception: accessing an inactive 
union member is not UB if the members are structs with initial common 
sequence.

Is this also true in a constexpr context? I ask this because the chapter 
for constexpr expression does not mention this exception accessing an 
inactive memeber.

Thanks for hints,
  Wilhelm

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Unions, common-initial subsequence and UB
  2022-10-20 11:46 Unions, common-initial subsequence and UB Wilhelm Meier
@ 2022-10-20 13:10 ` Wilhelm Meier
  0 siblings, 0 replies; 2+ messages in thread
From: Wilhelm Meier @ 2022-10-20 13:10 UTC (permalink / raw)
  To: gcc-help



On 20.10.22 13:46, Wilhelm Meier wrote:
> Hi all,
> 
> this is more a general question instead of a g++ specific one:
> 
> the standard explicitly makes this exception: accessing an inactive 
> union member is not UB if the members are structs with initial common 
> sequence.
> 
> Is this also true in a constexpr context? I ask this because the chapter 
> for constexpr expression does not mention this exception accessing an 
> inactive memeber.

Here is some test-code:

struct A {
     char e0;
};
struct B {
     char e0;
};

template<typename A, typename B>
union U {
     A a;
     B b;
};

int main() {
     constexpr U<A, B> u{.a = {1}}; // sets member a to active
     constexpr char t1 = u.b.e0; // should be ok, because read via a 
struct with common initial sequence
}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-20 13:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-20 11:46 Unions, common-initial subsequence and UB Wilhelm Meier
2022-10-20 13:10 ` Wilhelm Meier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).