我正在使用GCC版本8.3,并有一个开关声明:
#include <iostream>
enum class Type : char
{
Value1 = 'A',
Value2,
Value3,
Value4,
};
int main()
{
Type t;
switch(t)
{
case Type::Value1:
case Type::Value2:
case Type::Value3:
std::cout << "hw" << std::endl;
case Type::Value4:
break;
}
}我希望被迫使用[[fallthrough]],否则会生成警告。
我增加了gcc的旗帜,以发现丢失的枚举,并通过以下方式插入坠落:
g++ -o main.cc -O3 -Wswitch-enum -Wimplicit-fallthrough -std=c++1z新的标志已经被检测到了,因为我现在收到了丢失枚举的警告。我修复了这些,但我仍然没有收到任何关于隐式下降的警告。
我把密码放在这里:
https://godbolt.org/z/rj5sTPWz4
我是不是遗漏了什么?我还以为会被警告呢?
更新:
我将代码更改为:
struct Object
{
int a;
};
int main()
{
Type t;
Object o;
switch(t)
{
case Type::Value1:
case Type::Value2:
case Type::Value3:
case Type::Value4:
o.a = 5;
break;
}
}而且它仍然不会产生警告。
发布于 2022-05-13 16:13:12
我不认为如果在标签之间没有声明,这就不会被认为是错误的--这只是一个语句上的多个案例标签。
Gcc连警告都没有
case Type::Value1:
x++; //real fallthrough here
case Type::Value2:
case Type::Value3:
case Type::Value4:
break;虽然(不像clang),但它确实警告
case Type::Value1:
x++; //real fallthrough here
case Type::Value2:
x++;
case Type::Value3:
case Type::Value4:
break;https://godbolt.org/z/s8Ka84Gq6
因此,似乎你需要两个实际的落点( gcc和clang),并进入有影响的东西,以引起警告(对gcc)。
发布于 2022-05-13 16:05:48
case Type::Value4:
break;因为有突破,没有区别,如果你失败或失败。在那里加点东西。
case Type::Value3:
std::cout << "something";
case Type::Value4:
std::cout << "actually something else";
break;https://stackoverflow.com/questions/72232283
复制相似问题