【问题标题】:Why does std::forward_list::empty has [[nodiscard]] while std::forward_list::max_size doesn't? [duplicate]为什么 std::forward_list::empty 有 [[nodiscard]] 而 std::forward_list::max_size 没有? [复制]
【发布时间】:2020-07-18 16:28:42
【问题描述】:

std::forward_listdocumentation中,有两个成员函数:

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

让我吃惊的是:

为什么empty[[nodiscard]]max_size 没有?

【问题讨论】:

  • 我猜委员会还没有解决这个问题。似乎所有empty 方法都立即获得了它。

标签: c++ c++17 standards api-design forward-list


【解决方案1】:

原因分为两部分:

  1. 没有办法混淆查询“最大大小是多少?”表示为.maximum_size() 与其他任何内容,而您可能会混淆查询“它是空的吗?”表示为.empty(),使用命令“empty it!”,得到名称.clear()

  2. [[nodiscard]] 是新的,并且尚未在标准库中适用的所有地方应用(迄今为止)。 C++20 增加了一些地方,但还不够全面。

【讨论】:

  • 好吧,我可以想象有人可以阅读max_size() 来表示调整到最大支持的大小。我在想初学者有时如何写char my_input[99999]; 或类似的东西来“足够大,以防万一”。有人可能认为为他们的应用程序分配非常大的缓冲区是个好主意,并将max_size 视为为他们的目的而设计的功能。编辑:当然,这似乎比empty() 错误更罕见。
  • @FrançoisAndrieux 很好地证明了绝对的新秀确实令人难以置信。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 2012-08-01
  • 2017-07-07
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
相关资源
最近更新 更多