240426 C++ attribute [[nodiscard]]
https://stackoverflow.com/questions/76489630/explanation-of-nodiscard-in-c17
标记有返回值的函数,提醒程序员需要用函数的返回值,隐含的意思是函数不是直接作用在对象本身
Discussion
An example of good use for [[nodiscard]]
can be object methods that manipulate an object, but provide the result in a copy. Example:
1 | DateTime yesterday = DateTime.now().substractOneDay(); |
vs:
1 | DateTime yesterday = DateTime.now(); |
A [[nodiscard]]
would tell the programmer of the second example, that they are using it wrong.