An iterator shall not be implicitly converted to const_iterator
An iterator shall not be implicitly converted to const_iterator.
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) {...}The checker flags conversions from type iterator to
const_iterator or reverse_iterator to
const_reverse_iterator.
If you expect a rule violation but do not see it, refer to Coding Standard Violations Not Displayed.
| Group: Language support library |
| Category: Required, Automated |