gpt4 book ai didi

c++ - 属性可以应用于构造函数参数吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:21:38 26 4
gpt4 key购买 nike

Clang 8.0.0 和 GCC 9.1.0 似乎不同意这是否是有效代码。

struct Foo {
Foo([[maybe_unused]] int x) {}
};
int main() {}

Clang 不产生警告(即使使用 -Wall -Wextra -Wpedantic)但是 GCC 产生这个错误:

test.cpp:2:7: error: expected unqualified-id before '[' token
2 | Foo([[maybe_unused]] int x) {}
| ^
test.cpp:2:7: error: expected ')' before '[' token
2 | Foo([[maybe_unused]] int x) {}
| ~^
| )

那么哪个编译器有错误?

最佳答案

是的,它们可以应用。标准允许这样做。

10.6.6 Maybe unused attribute [dcl.attr.unused]
...
2 The attribute may be applied to the declaration of a class, a typedef-name, a variable, a non-static data member, a function, an enumeration, or an enumerator.

所以 Clang 在这里是正确的,这是一个 GCC 错误。已为此标题提交错误报告:maybe_unused attribute triggers syntax error when used on first argument to a constructor

关于c++ - 属性可以应用于构造函数参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56231168/

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