AutosarC++18_03-A23.0.1ΒΆ

An iterator shall not be implicitly converted to const_iterator

Required inputs: IR

Rationale

The Standard Template Library introduced methods for returning const iterators to containers. Making a call to these methods and immediately assigning the value they return to a const_iterator, removes implicit conversions.

Example
//% $Id: A23-0-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <vector>

void Fn1(std::vector<std::int32_t> &v) noexcept {
  for (std::vector<std::int32_t>::const_iterator iter{v.cbegin()},
       end{v.cend()};
       iter != end; ++iter) // Compliant
  {
    // ...
  }
}

void Fn2(std::vector<std::int32_t> &v) noexcept {
  for (auto iter{v.cbegin()}, end{v.cend()}; iter != end; ++iter) // Compliant
  {
    // ...
  }
}

void Fn3(std::vector<std::int32_t> &v) noexcept {
  for (std::vector<std::int32_t>::const_iterator iter{v.begin()}, end{v.end()};
       iter != end; ++iter) // Non-compliant
  {
    // ...
  }
}
See Also
  • HIC++ v4.0 [8]: 17.4.1 Use const container calls when result is immediately converted to a const iterator.

Possible Messages

Key

Text

Severity

Disabled

implicit_const_iterator

An iterator shall not be implicitly converted to const_iterator.

None

False

Options