Random access operator in MeshEntityIteratorBase modifies iterator state

Issue #484 resolved
Martin Sandve Alnæs created an issue

Iterators should behave as closely as possible to pointer arithmetic, i.e.:

int * a = some pointer;
int * b = a[pos]; // does not modify a
assert(b == a+pos);

In MeshEntityIteratorBase this code:

    /// Random access operator
    T& operator[] (std::size_t pos)
    { _pos = pos; return *operator->();}

modifies the iterator and behaves more like this pointer operation would do:

int * a = some pointer;
a = a+pos;  // modifies a
int * b = a;

This is misuse of standard operators and bug prone.

I would suggest just removing the random access operator. If it's used anywhere that code has a high chance of having bugs.

Comments (3)

  1. Log in to comment