Change return type of FieldIterator conversion (iterator to const_iterator)
In my code, I could not do the following
template< typename Field_T >
void foo( Field_T & field )
{
auto * buffer = field.clone();
for( typename Field_T::const_base_iterator it = field.begin(); ... )
{
buffer->( it.cell() ) = ... calculate something based on *it (plus some neighbors) ...
}
field.swapDataPointers( buffer );
}
because the compiler would not create a const_iterator (or const_base_iterator) from a non-const field. I'm not sure if this is a bug ( or a feature ;), but it would be handy to create a const_iterator from a non-const field in some situations, like the one above. Or can foo
be implemented in a better/more efficient way?
Proposal
If the custom FieldIterator conversion is changed from
operator const FieldIterator<const T, fieldFSize> & () const {
const FieldIterator<const T, fieldFSize> * ptr;
ptr = reinterpret_cast< const FieldIterator<const T, fieldFSize>* > ( this );
return *ptr;
}
to
operator FieldIterator<const T, fieldFSize> () {
FieldIterator<const T, fieldFSize> * ptr;
ptr = reinterpret_cast< FieldIterator<const T, fieldFSize>* > ( this );
return *ptr;
}
the problem disappears (at the cost of creating a copy of the iterator).