Skip to content

Commit da5f330

Browse files
authored
[clang-tidy] Adjust size-empty doc because C++11 size() is constant-time (#117629)
From C++11, a conforming `size()` method is guaranteed to be a constant-time function. `empty()` is not _generally_ more efficient than `size()`. It might even be implemented in terms of `size()`. ---- Notes: - Microsoft's STL had implemented `empty()` as `return size() == 0`, until May 2021: https://github.com/microsoft/STL/pull/1836/files - The time complexity of `size()` (specifically for `std::set`) was discussed by the library working group in 2007-2009: https://cplusplus.github.io/LWG/issue632
1 parent 33faa82 commit da5f330

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

clang-tools-extra/clang-tidy/readability/ContainerSizeEmptyCheck.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ namespace clang::tidy::readability {
1818
/// a call to `empty()`.
1919
///
2020
/// The emptiness of a container should be checked using the `empty()` method
21-
/// instead of the `size()` method. It is not guaranteed that `size()` is a
22-
/// constant-time function, and it is generally more efficient and also shows
23-
/// clearer intent to use `empty()`. Furthermore some containers may implement
24-
/// the `empty()` method but not implement the `size()` method. Using `empty()`
25-
/// whenever possible makes it easier to switch to another container in the
26-
/// future.
21+
/// instead of the `size()` method. It shows clearer intent to use `empty()`.
22+
/// Furthermore some containers may implement the `empty()` method but not
23+
/// implement the `size()` method. Using `empty()` whenever possible makes it
24+
/// easier to switch to another container in the future.
2725
class ContainerSizeEmptyCheck : public ClangTidyCheck {
2826
public:
2927
ContainerSizeEmptyCheck(StringRef Name, ClangTidyContext *Context);

clang-tools-extra/docs/clang-tidy/checks/readability/container-size-empty.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ Checks whether a call to the ``size()``/``length()`` method can be replaced
88
with a call to ``empty()``.
99

1010
The emptiness of a container should be checked using the ``empty()`` method
11-
instead of the ``size()``/``length()`` method. It is not guaranteed that
12-
``size()``/``length()`` is a constant-time function, and it is generally more
13-
efficient and also shows clearer intent to use ``empty()``. Furthermore some
14-
containers may implement the ``empty()`` method but not implement the ``size()``
15-
or ``length()`` method. Using ``empty()`` whenever possible makes it easier to
16-
switch to another container in the future.
11+
instead of the ``size()``/``length()`` method. It shows clearer intent to use
12+
``empty()``. Furthermore some containers may implement the ``empty()`` method
13+
but not implement the ``size()`` or ``length()`` method. Using ``empty()``
14+
whenever possible makes it easier to switch to another container in the future.
1715

1816
The check issues warning if a container has ``empty()`` and ``size()`` or
1917
``length()`` methods matching following signatures:

0 commit comments

Comments
 (0)