g++ fails to compile following code snippet:
namespace X {
enum En {A, B};
bool test(En e);
}
bool check() {
union {
struct {
X::En y:16;
X::En z:16;
} x;
int z;
} zz;
return test(zz.x.y);
}
The error it gives is following
In function 'bool check()': 15 : error: 'test' was not declared in this scope return test(zz.x.y); ^ 15 : note: suggested alternative: 3 : note: 'X::test' bool test(En e); ^~~~ Compilation failed
If i make y
a regular member, rather than a bitfield, code compiles successfully. Calling a name-spaced test
works as well. Clang compiles the program as-is without any complains.
Putting bitfield business aside (I do not love it at all, but codebase has it) and not focusing on whether I have a guarantee of fitting an enum into the 16-bit member or not, is there something special regarding bitfields which prevents ADL from kicking in as I expect it?
enum En : short
it compiles – Gerdienum En : int
doesn't compile either. Minimal example doesn't require theunion
to trigger the bug. The error is preceded by the warningwarning: ‘<anonymous struct>::y’ is too small to hold all values of ‘enum X::En’
. If the packed bitfield is large enough to hold the specified type (which defaults tounsigned int
) without truncation, it succeeds. It appears as if gcc chooses to ignore parameters which have been subject to truncation as valid hints to ADL. – Girdle