首页
学习
活动
专区
圈层
工具
发布

GCC
EN

Stack Overflow用户
提问于 2022-05-13 15:49:58
回答 2查看 92关注 0票数 1

我正在使用GCC版本8.3,并有一个开关声明:

代码语言:javascript
复制
#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的旗帜,以发现丢失的枚举,并通过以下方式插入坠落:

代码语言:javascript
复制
g++ -o main.cc -O3 -Wswitch-enum -Wimplicit-fallthrough -std=c++1z

新的标志已经被检测到了,因为我现在收到了丢失枚举的警告。我修复了这些,但我仍然没有收到任何关于隐式下降的警告。

我把密码放在这里:

https://godbolt.org/z/rj5sTPWz4

我是不是遗漏了什么?我还以为会被警告呢?

更新:

我将代码更改为:

代码语言:javascript
复制
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;
    }
}

而且它仍然不会产生警告。

EN

回答 2

Stack Overflow用户

发布于 2022-05-13 16:13:12

我不认为如果在标签之间没有声明,这就不会被认为是错误的--这只是一个语句上的多个案例标签。

Gcc连警告都没有

代码语言:javascript
复制
    case Type::Value1:
    x++; //real fallthrough here
    case Type::Value2:
    case Type::Value3:
    case Type::Value4:
        break;

虽然(不像clang),但它确实警告

代码语言:javascript
复制
    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)。

票数 2
EN

Stack Overflow用户

发布于 2022-05-13 16:05:48

代码语言:javascript
复制
    case Type::Value4:
        break;

因为有突破,没有区别,如果你失败或失败。在那里加点东西。

代码语言:javascript
复制
    case Type::Value3:
        std::cout << "something";
    case Type::Value4:
        std::cout << "actually something else";
        break;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72232283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档