我在 Clang-12 中偶然发现了一个奇怪的编译错误。下面的代码在 GCC 9 中编译得很好。这是编译器中的错误还是我的代码存在实际问题并且 GCC 太宽容了?
#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;
}
Run Code Online (Sandbox Code Playgroud)
如果我删除该行template<typename T>并只编写V v;而不是V<void> v;. x如果我将其设为非原子性,问题也会消失。我的编译命令是:
clang++-12 test.cpp -std=c++17 -o test
Run Code Online (Sandbox Code Playgroud)
我得到以下编译器输出:
test.cpp:18:9: error: case value is not a constant …Run Code Online (Sandbox Code Playgroud)