gpt4 book ai didi

c++ - gcc 和 __attribute__((unused)) 用于自动引用

转载 作者:太空狗 更新时间:2023-10-29 22:58:50 24 4
gpt4 key购买 nike

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

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

关于c++ - gcc 和 __attribute__((unused)) 用于自动引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39246593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com