在C++中,我们可以用"[[nodiscard]]"来修饰返回类型,如果结果未使用,则会触发编译器警告.
这对于执行错误代码特别有用
auto dont_forget_to_check = do_something_important();
assert(dont_forget_to_check);
C语言中有这样的东西吗?
在C++中,我们可以用"[[nodiscard]]"来修饰返回类型,如果结果未使用,则会触发编译器警告.
这对于执行错误代码特别有用
auto dont_forget_to_check = do_something_important();
assert(dont_forget_to_check);
C语言中有这样的东西吗?
没有标准的方法可以做到这一点,但是gcc确实支持warn_unused_result
属性.
__attribute__ ((warn_unused_result))
int foo()
{
return 5;
}
int main()
{
foo();
return 0;
}
编译器输出:
[dbush@db-centos7 ~]$ gcc -g -Wall -Wextra -o x1 x1.c
x1.c: In function ‘main’:
x1.c:11:8: warning: ignoring return value of ‘foo’, declared with attribute warn_unused_result [-Wunused-result]
foo();
^