Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
似乎empty()是唯一被标记为nodiscard.
empty()
nodiscard
其他类似的功能size()还没有。
size()
这是为什么 ?
首先,我希望将来会标记更多功能[[nodiscard]]。
[[nodiscard]]
但是,empty对于新用户来说,传统上是一个混淆点。他们认为这是一个动词,它会删除容器的所有元素(这里正确的调用是clear)。
empty
clear
所以标记empty为[[nodiscard]]捕获了人们称之为期望它对容器做某事的所有地方(而不是仅仅返回size() == 0)
size() == 0