AutosarC++17_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 271687 2017-03-23 08:57:35Z piotr.tanski $
#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