I stumbled upon a strange compile error in Clang-12. The code below compiles just fine in GCC 9. Is this a bug in the compiler or is there an actual problem with my code and GCC is just too forgiving?
#include<atomic>
enum X {
A
};
class U {
public:
std::atomic<X> x;
U() { x = A; }
};
template<typename T>
class V : public U {
public:
V() {
switch(x) {
case A:
break;
}
}
};
int main() {
V<void> v;
return 0;
}
The code also compiles fine in Clang-12 if I remove the template<typename T>
line and just write V v;
instead of V<void> v;
. The problem also goes away if I make x
non-atomic. My compile command is:
clang++-12 test.cpp -std=c++17 -o test
I get the following compiler output:
test.cpp:18:9: error: case value is not a constant expression
case A:
^
test.cpp:17:10: warning: enumeration value 'A' not handled in switch [-Wswitch]
switch(x) {
^
test.cpp:25:10: note: in instantiation of member function 'V<void>::V' requested here
V<void> v;
^
1 warning and 1 error generated.
this->x
. Interesting pair of diagnostics at any rate. – Meany