为什么在C ++ 20中std :: move没有[[nodiscard]]? [英] Why is std::move not [[nodiscard]] in C++20?
问题描述
我最近阅读了C ++ 17中的[[nodiscard]]
,据我了解,它是一项新功能(按合同设计?),它迫使您使用返回值.对于像std::launder
这样的有争议的函数(自C ++ 20起不丢弃),这是有道理的,但是我不知道为什么std::move
的定义没有像C ++ 17/20中那样.您知道很好的理由还是因为C ++ 20尚未完成?
I've recently read about [[nodiscard]]
in C++17, and as far as I understand it's a new feature (design by contract?) which forces you to use the return value. This makes sense for controversial functions like std::launder
(nodiscard since C++20), but I wonder why std::move
isn't defined like so in C++17/20. Do you know a good reason or is it because C++20 isn't finalised yet?
推荐答案
AFAIK P0600R1 is the only proposal for adding [[nodiscard]]
to the standard library that was applied to C++20. From that paper:
我们建议采取保守的方法:
We suggest a conservative approach:
[...]
在以下情况下不应添加:
It should not be added when:
- [...]
- 不使用返回值没有意义,但不会造成伤害,并且通常不是错误
- [...]
- [...]
- not using the return value makes no sense but doesn’t hurt and is usually not an error
- [...]
因此,[[nodiscard]]不应发出错误的代码
So, [[nodiscard]] should not signal bad code if this
- [...]
- 没有受到伤害,并且可能没有状态改变就意味着不会发生
因此,原因是标准库使用的是保守方法,而尚未提出更具综合性的方法.
So the reason is that the standard library uses a conservative approach and a more aggresive one is not yet proposed.
这篇关于为什么在C ++ 20中std :: move没有[[nodiscard]]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!