c++c++17standardsapi-designforward-list

Why does std::forward_list::empty has [[nodiscard]] while std::forward_list::max_size doesn't?


In the documentation of std::forward_list, there are two member functions:

  1. [[nodiscard]] bool empty() const noexcept;
  2. size_type max_size() const noexcept;

What makes me surprised is:

Why does empty has [[nodiscard]] while max_size doesn't?


Solution

  • The reason is two-part:

    1. There is no way to confuse the query "what is the maximum size?" expressed as .maximum_size() with anything else, while you could confuse the query "is it empty?" expressed as .empty() with the command "empty it!", which got the name .clear().

    2. [[nodiscard]] is new, and has not been applied everywhere applicable in the standard library (to date). C++20 adds some places, but still isn't anywhere near comprehensive.