| 1 |
.. title:: clang-tidy - readability-container-size-empty |
| 2 |
|
| 3 |
readability-container-size-empty |
| 4 |
================================ |
| 5 |
|
| 6 |
|
| 7 |
Checks whether a call to the ``size()`` method can be replaced with a call to |
| 8 |
``empty()``. |
| 9 |
|
| 10 |
The emptiness of a container should be checked using the ``empty()`` method |
| 11 |
instead of the ``size()`` method. It is not guaranteed that ``size()`` is a |
| 12 |
constant-time function, and it is generally more efficient and also shows |
| 13 |
clearer intent to use ``empty()``. Furthermore some containers may implement |
| 14 |
the ``empty()`` method but not implement the ``size()`` method. Using |
| 15 |
``empty()`` whenever possible makes it easier to switch to another container in |
| 16 |
the future. |
| 17 |
|
| 18 |
The check issues warning if a container has ``size()`` and ``empty()`` methods |
| 19 |
matching following signatures: |
| 20 |
|
| 21 |
.. code-block:: c++ |
| 22 |
|
| 23 |
size_type size() const; |
| 24 |
bool empty() const; |
| 25 |
|
| 26 |
`size_type` can be any kind of integer type. |