MAG*_*MAG 5 c++ naming stl naming-conventions
请std::vector从cplusplus.com检查。
它说它有一个empty()成员函数:
empty
Test whether vector is empty (public member function )
http://www.cplusplus.com/reference/vector/vector/empty/
Signature : bool empty() const;
Run Code Online (Sandbox Code Playgroud)
对我来说,空的含义应该是从这个容器中删除所有元素。然而,它是一个测试函数,用于检查容器是否为空。我检查了所有其他容器,所有容器都以同样的方式使用空。
为什么不使用 stlisEmpty()来代替?
is_empty()为了与 C++ 标准库的命名约定保持一致,我实际上将您所引用的成员函数命名为isEmpty(). 也就是说,遵循蛇形命名约定而不是驼峰命名约定。我们可以在其他成员函数中看到这种约定,例如push_back()和pop_back()。
Note that there is a member function called clear() that does empty the container. Perhaps the reason for omitting the verb (i.e., the is of a hypothetical is_empty), and therefore requiring less typing, was this separation of names for querying whether the container is empty (i.e., empty()) and emptying the container (i.e., clear()).
Otherwise, if the word "empty" would have been used instead, for both the query and the update, the verb would have been probably part of the name of the member function for querying, since both member functions have no parameters. Therefore overloading wouldn\'t have been possible. The member functions would have been then likely called empty() and is_empty() for emptying the container and for querying whether it is empty, respectively.
[[nodiscard]] attributeIn these cases where the name of a member function corresponds to both an adjective and a verb (e.g., member functions with the name empty, clear or clean), using the C++17\'s [[nodiscard]] attribute can be very useful to signal misbehavior.
例如,empty()如 \xe2\x80\x93 中执行查询的std::vector成员函数,因此返回值 \xe2\x80\x93 可以被标记为 ,这样如果不使用返回值,编译器会发出警告。这个警告表明程序员可能使用了好像确实清空了容器的操作,但实际上它是在查询容器是否为空。[[nodiscard]]empty()
但请注意,编译器不需要发出警告。
\n