1

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?

xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • 2
    I'd guess the committee hasn't got around to it yet. It appears all the [empty](https://en.cppreference.com/w/cpp/language/attributes/nodiscard) methods got it at once. – cigien Apr 06 '20 at 15:59

1 Answers1

4

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 1
    Well, I can imagine someone could read `max_size()` to mean resizing to the biggest supported size. I'm thinking of how beginners sometimes write `char my_input[99999];` or something like it to "be big enough, just in case". Someone might think it's a good idea to allocate very large buffers for their application and see `max_size` as a feature designed for their purpose. Edit : Granted it seems like a rarer mistake than getting `empty()` wrong. – François Andrieux Apr 06 '20 at 16:02
  • 1
    @FrançoisAndrieux Nice demonstration that absolute rookies can be truely unbelievable. – Deduplicator Apr 06 '20 at 16:05