| 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()``/``length()`` method can be replaced |
| 8 |
with a call to ``empty()``. |
| 9 |
|
| 10 |
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. |
| 17 |
|
| 18 |
The check issues warning if a container has ``empty()`` and ``size()`` or |
| 19 |
``length()`` methods matching following signatures: |
| 20 |
|
| 21 |
.. code-block:: c++ |
| 22 |
|
| 23 |
size_type size() const; |
| 24 |
size_type length() const; |
| 25 |
bool empty() const; |
| 26 |
|
| 27 |
`size_type` can be any kind of integer type. |
| 28 |
|
| 29 |
.. option:: ExcludedComparisonTypes |
| 30 |
|
| 31 |
A semicolon-separated list of class names for which the check will ignore |
| 32 |
comparisons of objects with default-constructed objects of the same type. |
| 33 |
If a class is listed here, the check will not suggest using ``empty()`` |
| 34 |
instead of such comparisons for objects of that class. |
| 35 |
Default value is: `::std::array`. |