| High Integrity CPP Guideline 17.16 | Minimise mixing of iterator types. |
| Justification |
Iterator types are implementation defined. Portability issues may arise as different STL implementations may have different operations defined for particular iterators. Efficiency may suffer where different iterator types are used as operands in operator expressions. Potentially the operator is a function call for which one or both of the iterators must undergo a conversion. Certain container member functions may not be called as they only accept the plain iterator type as a parameter.
// May or may not be a member in some implementations.
//
template< typename T >
bool operator== ( vector< T >::const_iterator& lhs,
vector< T >::const_iterator& rhs );
void bar()
{
vector< int > v;
vector< int >::iterator lhs = v.begin();
vector< int >::const_iterator rhs = v.end();
// Should operator== be implemented
// as a member of const_iterator then
// this this code will not compile.
//
// rhs implicitly converted
// to const_iterator followed by
// function call to operator ==.
//
if ( lhs == rhs )
{}
}
void foo( vector< int >& v,
vector< int >::const_iterator& iter )
{
// Error cannot convert from
// const_iterator to iterator.
//
v.insert( iter, 10 );
}
|
| Reference |
Effective STL Item 26; |