问题描述
C++17 添加了[[nodiscard]]
.
C++20 在 empty
方法上添加了 [[nodiscard]]
的使用,例如vector::empty()
-- 也许, 是为了避免用户对 clear 方法的混淆(即意外调用 empty() 以clear 向量).
C++20 added the use of [[nodiscard]]
on empty
methods, e.g. vector::empty()
-- maybe, to avoid user confusion with the method clear (i.e. calling empty() accidentally to clear the vector).
为什么 C++20 没有利用这个机会将 [[nodiscard]]
添加到 unique_ptr::release
中?
Why didn't C++20 use this opportunity to add [[nodiscard]]
to unique_ptr::release
?
是否存在一种有效的合理场景,可以调用 unique_ptr::release
不取返回值?
Is there a valid reasonable scenario in which one would call unique_ptr::release
without taking the returned value?
以同样的方式避免用户混淆(如果这是将 [[nodiscard]]
添加到 empty
方法的原因) - 名称 release
总是很混乱,听起来像,嗯……这里会发布一些东西.
In the same manner of avoiding user confusion (if this was the reason for adding [[nodiscard]]
to the empty
methods) - the name release
was always very confusing, sounds like, well... something is going to be released here.
添加 [[nodiscard]]
可以在某种程度上解决这个名称问题.
Adding [[nodiscard]]
could fix this name issue, in a way.
推荐答案
这在将 [[nodiscard]]
添加到许多函数的论文中得到了解决.来自 P0600R1 这是关于将 [[nodiscard]]
添加到 unique_ptr::release()
This is addressed in the paper that added [[nodiscard]]
to many of the functions. From P0600R1 this is the remark about adding [[nodiscard]]
to unique_ptr::release()
Titus:在谷歌,3.5% 的调用会失败,但分析显示这是正确的(但奇怪的所有权语义).看反射电子邮件.
这篇关于为什么unique_ptr::release 没有用[[nodiscard]] 定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!