当我想念[[[Fallthrough]]]时

发布于 2025-01-28 14:05:29 字数 1131 浏览 4 评论 0 原文

我正在使用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;
    }
}

它仍然不会产生警告。

I am using GCC version 8.3 and have a switch statement:

#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;
    }
}

I want to be forced to use [[fallthrough]] otherwise generate a warning.

I added gcc flags to detect missing enums and implict fallthrough:

g++ -o main.cc -O3 -Wswitch-enum -Wimplicit-fallthrough -std=c++1z

The new flags have been detected because I now get warnings I'm missing enums. I fixed those but I still receive no warnings about implicit fallthrough.

I've put the code here:

https://godbolt.org/z/rj5sTPWz4

Am I missing something? I was expecting to be warned?

UPDATE:

I changed the code to this:

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;
    }
}

and it still doesn't generate a warning.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我恋#小黄人 2025-02-04 14:05:29

我认为,如果您之间的标签之间没有陈述,那就不会被认为是秋天的,这只是声明中的多个案例标签。

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;

因此,似乎您似乎需要实际的摔倒(在GCC和Clang上),并陷入有效的东西以引起警告(在GCC上)。

I don't think it's considered fallthrough if you've got no statements in between the labels—that's just multiple case labels on a statement.

Gcc doesn't even warn on

    case Type::Value1:
    x++; //real fallthrough here
    case Type::Value2:
    case Type::Value3:
    case Type::Value4:
        break;

though (unlike clang), but it does warn on

    case Type::Value1:
    x++; //real fallthrough here
    case Type::Value2:
    x++;
    case Type::Value3:
    case Type::Value4:
        break;

https://godbolt.org/z/s8Ka84Gq6

so it seems you need both actual fallthrough (on both gcc and clang) and into something that has an effect in order to elicit the warning (on gcc).

茶色山野 2025-02-04 14:05:29
    case Type::Value4:
        break;

因为只是在那里破裂,所以无论您是否跌倒,都没有区别。在那里添加一些东西。

    case Type::Value3:
        std::cout << "something";
    case Type::Value4:
        std::cout << "actually something else";
        break;
    case Type::Value4:
        break;

Because there is just break there, there is no difference, if you fall through or not. Add something there.

    case Type::Value3:
        std::cout << "something";
    case Type::Value4:
        std::cout << "actually something else";
        break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文