AUTOSAR C++14 Rule A23-0-1
Description
Rule Definition
An iterator shall not be implicitly converted to const_iterator.
Rationale
The C++11 standard introduces member functions such as cbegin
and
cend
that returns const iterators to containers. To create const
iterators, use these member functions instead of functions such as begin
and end
that return non-const iterators and then require implicit
conversions.
For instance, consider the std::list
container:
std::list<int> aList = {0, 0, 1, 2};
begin
and end
member functions of the
container to create const iterators, for instance in a for
loop:for(std::vector<int>::const_iterator iter{aList.begin()}, end{aList.end()}; iter != end; ++iter) {...}
begin
and end
return non-const iterators
and for assignment to the const iterators iter
and end
respectively, an implicit conversion must happen. Instead, take advantage of the new C++11
functions cbegin
and cend
that directly returns const
iterators:for(std::vector<int>::const_iterator iter{aList.cbegin()}, end{aList.cend()}; iter != end; ++iter) {...}
auto
:for(auto iter{aList.cbegin()}, end{aList.cend()}; iter != end; ++iter) {...}
Polyspace Implementation
The checker flags conversions from type iterator
to
const_iterator
or reverse_iterator
to
const_reverse_iterator
.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Containers library |
Category: Required, Automated |
Version History
Introduced in R2020a