c++ - 为什么即使我使用 [[fallthrough]],GCC 也会警告我失败?

标签 c++ switch-statement c++17 fall-through

在下面的代码中,我使用标准 [[fallthrough]]来自 C++1z 的属性以记录需要失败:

#include <iostream>

int main() {
    switch (0) {
        case 0:
            std::cout << "a\n";
            [[fallthrough]]
        case 1:
            std::cout << "b\n";
            break;
    }
}

使用 GCC 7.1,代码编译时不会出错。但是,编译器仍然会警告我失败:

warning: this statement may fall through [-Wimplicit-fallthrough=]
    std::cout << "a\n";
    ~~~~~~~~~~^~~~~~~~

为什么?

最佳答案

属性后面少了一个分号:

case 0:
    std::cout << "a\n";
    [[fallthrough]];
    //             ^
case 1:

[[fallthrough]] 属性将应用于空语句(参见 P0188R1 )。当前 Clang 主干 gives a helpful error in this case :

error: fallthrough attribute is only allowed on empty statements
    [[fallthrough]]
      ^
note: did you forget ';'?
    [[fallthrough]]
                   ^
                   ;

更新:科迪·格雷 reported这个问题交给 GCC 团队。

关于c++ - 为什么即使我使用 [[fallthrough]],GCC 也会警告我失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45026747/

相关文章:

javascript - 如何使用 react 动态导入组件?/动态导入

c# - 避免使用 if-else 或 switch-case 语句来进行决定

C++ 模板化模板推导可以回避吗?

c++ - 复制/移动构造函数可以通过 C++17 中的 using 声明来继承吗?

c++ - 具有统一初始化的自动扩展为意外类型

c++ - 从 Maple 到 C++ 的翻译

c++ - ˋtypedefˋ 中的异常规范是完全禁止的还是仅在顶层?

c++ - 如何模板化变量名称,而不是类型?

C - 战舰,随机放置

c++ - 模板类的模板嵌套类作为完全专用函数的参数