The MSVC standard library team went ahead and added several thousand instances of [[nodiscard]]
since VS 2017 15.6, and have reported wild success with it (both in terms of finding lots of bugs and generating no user complaints). The criteria they described were approximately:
- Pure observers, e.g.
vector::size()
, vector::empty
, and even std::count_if()
- Things that acquire raw resources, e.g.
allocate()
- Functions where discarding the return value is extremely likely to lead to incorrect code, e.g.
std::remove()
MSVC does mark both std::move()
and std::forward()
as [[nodiscard]]
following these criteria.
While it's not officially annotated as such in the standard, it seems to provide clear user benefit and it's more a question of crafting such a paper to mark all the right things [[nodiscard]]
(again, several thousand instances from MSVC) and apply them -- it's not complex work per se, but the volume is large. In the meantime, maybe prod your favorite standard library vendor and ask them to [[nodiscard]]
lots of stuff?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…