c++ - g++如何在忽略函数返回值时获得警告

标签 c++ gcc g++ return-value compiler-warnings

lint 会产生一些警告,例如:

foo.c XXX Warning 534: Ignoring return value of function bar()

来自 lint manual

534 Ignoring return value of function

'Symbol' (compare with Location) A function that returns a value is called just for side effects as, for example, in a statement by itself or the left-hand side of a comma operator. Try: (void) function(); to call a function and ignore its return value. See also the fvr, fvo and fdr flags in §5.5 "Flag Options".

我想在编译期间收到此警告(如果存在)。 gcc/g++ 中是否有任何选项可以实现这一点?我打开了 -Wall 但显然没有检测到这一点。

最佳答案

从 C++17 开始,您可以使用 [[nodiscard]] attribute .

例子:

[[nodiscard]] int bar() {
  return 42;
}

关于c++ - g++如何在忽略函数返回值时获得警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2870529/

相关文章:

c++ - 是否有任何 g++ 选项可以转储类布局和 vtables?

C++ 错误 : ‘string’ has not been declared

c++ - 作为 C++ 类的成员启动线程的最佳方式?

c++ - C block 扩展 (libBlocksRuntime) - 为 Block_copy() 使用自定义内存分配器 (Boehm GC)

c++ - 使用 Ifstream 读取多行?

c++ - 我可以传递给函数的最大字符串长度(或大小)是多少?

c++ - 启用 C++11 时的 std::vector 性能回归

C++ Makefile编译顺序

ubuntu - 在 ubuntu 上构建错误 gcc-5.1.0 :

c - 为什么 "1 ? (int*)1 : ((void*)((x) * 0l))"可以正常工作?