Consider these two functions:
int f1()
{
alignas(int) char buf[sizeof(int)] = {};
return *reinterpret_cast<int*>(buf);
}
int f2()
{
alignas(int) char buf[sizeof(int)] = {};
char* ptr = buf;
return *reinterpret_cast<int*>(ptr);
}
GCC warns that the first violates strict-aliasing rules. But the second is OK.
Clang accepts both without complaint.
Is the warning legitimate?
The warning is legitimate.
f2
is not OK (it is undefined behaviour), it just doesn't provoke the warning.I suspect the reason that
f2
doesn't provoke the warning is that:Is entirely legal. You can use a
char*
(or avoid*
) as a "universal pointer" - provided you cast back to the right type before access. GCC is clearly being careful to avoid warning aboutf3
, at a cost of not warning aboutf2
.Clang is failing to warn about either
f1
orf2
- but it is not required to.