* Undefined behaviour in a small code snippet
@ 2018-03-13 0:10 Łukasz Szczur
0 siblings, 0 replies; only message in thread
From: Łukasz Szczur @ 2018-03-13 0:10 UTC (permalink / raw)
To: gcc-help
Hello,
The program at the bottom prints
1
2
1
2
Does the code contain undefined behaviour in C++17 (note that the structs
have common initial sequence )?
Łukasz
#include<iostream>
using namespace std;
struct A {
int first;
int second;
};
struct B {
int first;
int second;
};
struct Test {
union {
A a;
B b;
};
};
int main() {
Test t;
t.a.first = 1;
t.b.second = 2;
cout << t.a.first << '\n';
cout << t.a.second << '\n';
cout << t.b.first << '\n';
cout << t.b.second << '\n';
return 0;
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2018-03-13 0:10 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-13 0:10 Undefined behaviour in a small code snippet Łukasz Szczur
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).