gcc 和 __attribute__((unused)) 用于自动引用

gcc and __attribute__((unused)) for auto references

将 gcc(已测试 5.4.0 和 6.1.1)与 -Wall 结合使用,以下给出了有关 auto_ref 未使用变量的警告,但不会针对其他变量发出警告。 Clang 不会发出任何警告。 auto& 变量的这种差异是有意的吗?为什么?

int main() {
    int __attribute__((unused)) int_var_unused = 42;
    int int_var = 42;
    int& __attribute__((unused)) int_ref = int_var;
    auto __attribute__((unused)) auto_var_unused = 42;
    auto auto_var = 42;
    auto& __attribute__((unused)) auto_ref = auto_var;
    return 0;
}

不确定这是否是 GCC 中的错误,但它是这样工作的

__attribute__((unused)) auto& auto_ref = auto_var;

像这样

auto& auto_ref __attribute__((unused)) = auto_var;

我想属性永远不会放在类型声明和名称之间。在文档中,我主要以第二个版本为例。