5

It seems like empty() is the only function that has been marked as nodiscard.

Other similar functions like size() have not.

Why is that?

Pharap
  • 3,826
  • 5
  • 37
  • 51
Fourmet
  • 538
  • 2
  • 12

1 Answers1

9

First, I would expect that more functions will be marked [[nodiscard]] in the future.

However, empty is traditionally a point of confusion for new users. They think that it's a verb, and that it will remove all the elements of a container (the correct call here is clear).

So marking empty as [[nodiscard]] catches all the places where people call it expecting it to do something to the container (instead of just returning size() == 0)

Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
  • Updating a few million line codebase hit this about 5 times. Mostly other containers with `Empty` translated to std containers at some point. Annoyingly, at least one was a mandatory failure (ie, the error was relied upon by the code). Sigh. – Yakk - Adam Nevraumont Nov 25 '19 at 01:12
  • While I hope that great library implementers mark many more functions as ```[[nodiscard]]```, I wouldn't expect much more in terms of the standard doing so. Technically, it has no normative effect, and practically speaking, the more time the committee spends debating which functions should be marked ```[[nodiscard]```, the less time the committee spends on other desirable features. – Nevin Nov 25 '19 at 22:00