What are usecases in which it is beneficial to use a [[nodiscard]]
type?
On types, [[nodiscard]]
emits a warning if the return value of any function returning an instance of that type is omitted; (Citation from p0068r0):
If [[nodiscard]] is marked on a type, it makes it so that all functions that return that type are implicitly [[nodiscard]].
while a [[nodiscard]]
constructor (c++2a) is very useful for classes which manage resources (e.g. unique_ptr
) and nodiscard for functions are for instance useful for make_unique
I cannot come up with an example where a nodiscard for a type is useful and I'm interested in cases where it is used.