如何有意丢弃[[nodiscard]]返回值? [英] How can I intentionally discard a [[nodiscard]] return value?

查看:786
本文介绍了如何有意丢弃[[nodiscard]]返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有

[[nodiscard]] int foo ()
{
    return 0;
}

int main ()
{
    foo ();
}

然后

error: ignoring return value of ‘int foo()’, declared with attribute nodiscard [-Werror=unused-result]

,但如果

int x = foo ();

然后

error: unused variable ‘x’ [-Werror=unused-variable]

是否存在告诉编译器我要舍弃此 [[nodiscard]] 值的一种干净方法?

Is there a clean way of telling the compiler "I want to discard this [[nodiscard]] value"?

推荐答案

WG14 nodiscard提案讨论了通过强制转换为无效来使诊断静音的理由。它说强制转换为空是鼓励(如果不是非规范性的)使它静音的方法,该方法遵循现有实现对 __ attribute __((warn_unused_result))的作用:

The WG14 nodiscard proposal discusses the rationale for allowing the diagnostic to be silenced by casting to void. It says casting to void is the encouraged (if non-normative) way to silence it which follows what the existing implementation do with __attribute__((warn_unused_result)):


[[nodiscard]]属性在现实世界中具有广泛的用途,由Clang和GCC实施为__attribute __((warn_unused_result))
,但由WG21以[[nodiscard]]名称进行了标准化。这项建议选择标识符nodiscard
,因为与此名称的偏离会导致与C ++不必要的不​​兼容。

The [[nodiscard]] attribute has extensive real-world use, being implemented by Clang and GCC as __attribute__((warn_unused_result)) , but was standardized under the name [[nodiscard]] by WG21. This proposal chose the identifier nodiscard because deviation from this name would create a needless incompatibility with C++.

此属性的语义很大程度上取决于a的概念。用途,
的定义由实施人员自行决定。但是,WG21指定的非规范性指南中的
鼓励
实现在可能评估的舍弃值表达式中使用nodiscard函数
调用时发出警告诊断。
,除非它是明确转换为空的
。这意味着不鼓励
实现执行数据流分析(就像
初始化但未使用的局部变量诊断一样)。
...

The semantics of this attribute rely heavily on the notion of a use, the definition of which is left to implementation discretion. However, the non-normative guidance specified by WG21 is to encourage implementations to emit a warning diagnostic when a nodiscard function call is used in a potentially-evalulated discarded-value expression unless it is an explicit cast to void. This means that an implementation is not encouraged to perform dataflow analysis (like an initialized-but- unused local variable diagnostic would require). ...

C ++方式为 static_cast< void>

请参阅C ++标准草案[[ dcl.attr.nodiscard] p2

See the draft C++ standard [[dcl.attr.nodiscard]p2:


[注意:无丢弃调用是一个函数调用表达式,该函数调用先前声明为nodiscard的函数,或者其返回类型是可能的cv限定类或标记为nodiscard的枚举类型。
不建议将nodiscard调用显示为可能评估的舍弃值表达式,除非明确将其强制转换为void。
在这种情况下,实现应发出警告。
这通常是因为丢弃nodiscard调用的返回值会产生令人惊讶的后果。
-注释[end note]

[ Note: A nodiscard call is a function call expression that calls a function previously declared nodiscard, or whose return type is a possibly cv-qualified class or enumeration type marked nodiscard. Appearance of a nodiscard call as a potentially-evaluated discarded-value expression is discouraged unless explicitly cast to void. Implementations should issue a warning in such cases. This is typically because discarding the return value of a nodiscard call has surprising consequences. — end note]

这是一条注释,因此不是规范性的,但基本上这是现有实现对 __ attribute __((warn_unused_result))。另外,请注意,对 nodiscard 的诊断也是非规范的,因此,对违反 nodiscard 的诊断不是错误的形式,而是实现质量,就像通过强制转换来抑制一样

This is a note, so non-normative but basically this is what existing implementations do with __attribute__((warn_unused_result)). Also, note a diagnostic for nodiscard is also also non-normative, so a diagnostic for violating nodiscard is not ill-formed but a quality of implementation just like suppressing via a cast to void is.

参见在nodiscard上提交的clang文档,warn_unused_result


Clang支持诊断a的结果何时的能力。在可疑的情况下,将丢弃函数调用表达式。当函数或其返回类型标记为[[nodiscard]](或__attribute __((warn_unused_result)))并且函数调用显示为可能评估的舍弃表达式(未明确转换)时,将生成诊断。无效。

这篇关于如何有意丢弃[[nodiscard]]返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆