Let's start immediately with the example code:
#include <stdio.h>
#include <stdbool.h>
typedef union {
bool b;
int i;
} boolIntUnion;
int main(void) {
boolIntUnion val;
val.i = 0;
printf("i = %d; %s\n", val.i, ((val.b) ? "true" : "false"));
val.i = 1;
printf("i = %d; %s\n", val.i, ((val.b) ? "true" : "false"));
val.i = 2;
printf("i = %d; %s\n", val.i, ((val.b) ? "true" : "false"));
}
My question now if using the union as you can see here is some sort of undefined behaviour. What's of interest for me is the third case, where I'm setting val.i
to 2
, but then use the boolean from the union via val.b
as a condition for my printed string. In MSVC 19 and gcc, I get the expected behaviour of the 2
in the union resulting in a value of true
from val.b
, but with Clang (and Clang-cl), I get false
. I'm suspecting a compiler error, because if I look at the assembly output of Clang, I see test cl, 1
, whereas MSVC and gcc give me test eax, eax
and test al, al
respectively. I tested this on Godbolt with all three compilers including execution output. Clang in the middle is where you can see the differing behaviour. [1]
Now, I'm not sure if using a union in this way is undefined behaviour under any C or C++ standard. That's basically my question now. If this is valid code, which I assume, I'd file a bug with Clang / LLVM. Just wanted to be sure beforehand. I tested this with both Clang and Clang++, same behaviour.