- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果用户不保存某个函数的返回值,我想警告用户,但如果函数返回一个具有非默认析构函数的类,它就不起作用。
假设以下代码:
struct A {};
struct B { ~B() {} };
[[ gnu::warn_unused_result ]]
A foo() { return A{}; }
[[ gnu::warn_unused_result ]]
B bar() { return B{}; }
int main()
{
foo(); // warning
bar(); // no warning
return 0;
}
使用 g++ 6.2.1 编译时,只有 foo()
会产生警告:
$ g++ -Wall -Wextra ./test.cpp
./test.cpp: In function ‘int main()’:
./test.cpp:13:9: warning: ignoring return value of ‘A foo()’, declared with attribute warn_unused_result [-Wunused-result]
foo();
^
我知道调用 B
的析构函数可能被认为是“结果的使用”,但问题是:
[[ nodiscard ]]
的行为是否相同?最佳答案
Is this an expected behaviour or is this a bug/feature in g++?
这是一个 QoI 问题,所有此类警告也是如此。 gcc 7 确实为 bar()
发出警告,因此虽然不发出警告不是错误,但它更像是 gcc 6.2 中缺少的功能。
Will C++17's
[[ nodiscard ]]
behave the same?
这也是实现定义的。 [dcl.attr.nodiscard]中的标准中有一个非规范的注释:
Appearance of a nodiscard call as a potentially-evaluated discarded-value expression (Clause 5) is discouraged unless explicitly cast to void. Implementations are encouraged to issue a warning in such cases. This is typically because discarding the return value of a nodiscard call has surprising consequences.
在这种情况下,不需要实现来发出警告。但他们可能会尝试这样做。
关于c++ - 如果返回类具有析构函数,则忽略 gnu::warn_unused_result 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40551304/
GCC 支持使用属性 warn_unused_result 声明方法,这样任何不保存方法返回值的调用者都会收到警告。 Java 有没有类似的功能? 最佳答案 不,Java 没有这样的功能。 这可以通过
我目前正在研究一个 React Native 项目。从字面上看,我刚刚开始在 XCode 中遇到这个错误。 我已经做了大量的谷歌搜索无济于事。有没有人经历过这个?更重要的是,有人有解决方案吗? 最佳答
在一些新项目中愉快地使用了 __attribute__((warn_unused_result)) 一段时间。我一直在使用 Vim 进行编码。 现在我有时在开发同一个项目时开始使用 KDevelop,
我一直在努力寻找这在我的代码中不起作用的原因 - 我认为这应该起作用。以下是头文件的摘录: #define WARN_UNUSED __attribute__((warn_unused_res
我们开始在我们的 Objective-C 代码中对所有 becomeFirstResponder 函数调用发出此警告。 Ignoring return value of function declar
在测试中,我丢弃了 stderr 中的任何内容,因为它会扰乱测试用例的输出。我正在使用以下代码: freopen("/dev/null", "w", stderr); 使用-Wall -Werror
谁能用 warn_unqualified_access 和 warn_unused_result 的一个例子来解释一下 最佳答案 @warn_unused_result 假设您有一个表示一副纸牌的数组
我发现 __attribute__ ((warn_unused_result)) 作为一种鼓励开发人员不要忽略函数返回的错误代码的方法非常有用,但我需要它与 MSVC 以及gcc 和 gcc 兼容的编
如果用户不保存某个函数的返回值,我想警告用户,但如果函数返回一个具有非默认析构函数的类,它就不起作用。 假设以下代码: struct A {}; struct B { ~B() {} }; [[ gn
是否可以告诉 GCC 对所有函数使用 warn_unused_result 标志,即使没有相应的属性?因为如果我忘记检查返回值,我也可能忘记添加 GCC 特定属性。 我已经看到它可以与其他一些编译器一
我使用 Ideone.com 编译了下面的代码,弹出了以下警告。 rtctimer.c: In function 'rtctimer_next_tick': rtctimer.c:87.7: warn
考虑以下代码: struct MyType{}; MyType myFunction() __attribute__ ((warn_unused_result)) { return MyTyp
我很好奇,我在 PuTTy 上用 C 编程,有人知道我怎样才能摆脱这个警告吗? warning: ignoring return value of ‘realloc’, declared with a
#include int main() { int t; scanf("%d", &t); printf("%d", t); return 0; } 我使用 ideo
#include int main() { int t; scanf("%d", &t); printf("%d", t); return 0; } 我使用 ideo
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
const char *p; p="curl -d \'{\"switch\": \"00:00:3a:46:3b:8d:df:40\", \"name\":\"flow\", \"cookie\":
当我编译以下程序时:g++ -O2 -s -static 2.cpp它给了我警告 ignoring return value of ‘int scanf(const char*, ...)’, dec
我正在解决一个问题。问题是这样的:吉尔和荣格喜欢幸运数字。幸运数字显然是4和7,如果正数的十进制表示包含4和7,则该正整数被认为是幸运的。例如 444 、 474 和 7 是幸运的,而 4773 和
我有一个桥接到 Objective-C 的 Swift 类。即使使用 @discardableResult,它也会在 Objective-C 类的构建中抛出此警告: Ignoring return v
我是一名优秀的程序员,十分优秀!