2016-05-19 17:29:46 +08:00
|
|
|
.. title:: clang-tidy - performance-implicit-cast-in-loop
|
|
|
|
|
2016-01-29 23:21:32 +08:00
|
|
|
performance-implicit-cast-in-loop
|
|
|
|
=================================
|
|
|
|
|
2016-04-01 07:09:42 +08:00
|
|
|
This warning appears in a range-based loop with a loop variable of const ref
|
|
|
|
type where the type of the variable does not match the one returned by the
|
2016-08-27 01:46:51 +08:00
|
|
|
iterator. This means that an implicit cast has been added, which can for example
|
|
|
|
result in expensive deep copies.
|
2016-01-29 23:21:32 +08:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2016-08-27 01:46:51 +08:00
|
|
|
.. code-block:: c++
|
2016-01-29 23:21:32 +08:00
|
|
|
|
2016-08-27 01:46:51 +08:00
|
|
|
map<int, vector<string>> my_map;
|
|
|
|
for (const pair<int, vector<string>>& p : my_map) {}
|
|
|
|
// The iterator type is in fact pair<const int, vector<string>>, which means
|
|
|
|
// that the compiler added a cast, resulting in a copy of the vectors.
|
2016-01-29 23:21:32 +08:00
|
|
|
|
2016-04-02 09:07:18 +08:00
|
|
|
The easiest solution is usually to use ``const auto&`` instead of writing the type
|
2016-01-29 23:21:32 +08:00
|
|
|
manually.
|