问题描述
我有一些使用 GCC 扩展 [[gnu::warn_unused_result]]
(又名 __attribute__((__warn_unused_result__))
)的代码。现在我尝试使用 C2x 的 [[nodiscard]]
并且我得到了一个难以理解的错误。我不确定 [[nodiscard]]
的用法是否与旧的 GCC 属性有很大不同,或者它是否只是 GCC 中的一个错误:
$ cat warn_unused_result.c
[[gnu::warn_unused_result]]
int foo(void);
[[gnu::warn_unused_result]]
int bar(void);
int foo(void)
{
return 1;
}
int bar(void)
{
return foo();
}
$ cc -Wall -Wextra -Werror -std=c2x -c warn_unused_result.c
$ cat nodiscard.c
[[nodiscard]]
int foo(void);
[[nodiscard]]
int bar(void);
int foo(void)
{
return 1;
}
int bar(void)
{
return foo();
}
$ cc -Wall -Wextra -Werror -std=c2x -c nodiscard.c
nodiscard.c:2:1: error: 'nodiscard' attribute directive ignored [-Werror=attributes]
2 | int foo(void);
| ^~~
nodiscard.c:5:1: error: 'nodiscard' attribute directive ignored [-Werror=attributes]
5 | int bar(void);
| ^~~
cc1: all warnings being treated as errors
$ cc --version
cc (Debian 10.2.1-6) 10.2.1 20210110
copyright (C) 2020 Free Software Foundation,Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or fitness FOR A PARTIculaR PURPOSE.
显然,我没有忽略任何函数的返回值。
那个 1
永远不会丢失(bar()
的调用者在不同的翻译单元中,这可能是触发 GCC 行为的问题?...)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)