【问题标题】:gcc and __attribute__((unused)) for auto referencesgcc 和 __attribute__((unused)) 用于自动引用
【发布时间】:2016-08-31 10:04:31
【问题描述】:

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

【问题讨论】:

    标签: c++ gcc reference auto


    【解决方案1】:

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

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

    像这样

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 2011-12-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      相关资源
      最近更新 更多