2

Lets assume the code doesn't need the index, but something interesting happened during debugging and I want to know the position of the item in the container.

for (int item : myarray)
    doSomething(item); // <-- breakpoint/exception/assertion; what index was 'item'?

Lets assume it's a random access containers where it should still be resonable to find the answer at runtime. Is this possible? Can it be possible?

It'd be really convenient if we could e.g. hover over for and see the current iterator and its distance to the container's begin().

If item were a reference I could make a risky guess that the index is &item - &myarray[0]. Although it'd be a pain to type every time I wanted to check during debugging.


To clarify, there are many questions about getting the index in the code (interesting references here and here). Instead I want the index in the debugger. Maybe I didn't write the code, can't easily recompile or a repro is highly intermittent. It's perfectly reasonable to not want the index in the code because it can add clutter and confusion when it's not needed. Constructs such as std::views::zip and std::views::enumerate will make working without indices easier. That said, we currently lose debugging information when buying into these features.

5
  • 1
    Have you tried debugging the assembly language? The loop has to know the index somehow, but the index is hidden. The index should show up in the assembly language. Likewise, the limit is also hidden. Although, this structure of a for loop gives the compiler a lot of leeway with indices and limits. Commented Apr 3 at 20:06
  • 1
    @ThomasMatthews range-for loops use iterators only, not indexes. There is no index for the loop's assembly code to keep track of Commented Apr 3 at 21:15
  • @RemyLebeau I think the assumption is that the iterator uses an index internally. Of course, some iterators may not be index-based, e.g. a linked list.
    – Barmar
    Commented Apr 3 at 21:27
  • The question assumes a random-access container, but a std::deque would be a typical non-trivial case. It has O(1) distance, but the debugger would need to calculate that on demand. Conceptually feasible, but quite a bit of work to implement.
    – MSalters
    Commented Apr 4 at 7:14
  • I don't think the index is relevant. You have access to the object item. If you can pull all properties of that object, you can write a unit test in which you call doSomething(item) and reproduce the problem without a loop. No need for the index. Commented Apr 5 at 16:53

1 Answer 1

-1

Just in case when intention is to implement some functionality iterrable by foreach, and need to keep track of the current iteration. Starting from the simple idea of using pointers:

class myarray {
    int x[5] = { 1, 2, 3, 4, 5 };
public:
    int* begin() { return x; }
    int* end() { return x + 5; }
};

int main() {
    for (auto x : a) cout << x << endl;
    return 0;
}

The foreach will advance the pointer returned by begin calling operator ++.
So we wrap the concept of pointer in the concept of iterator to keep track of position:

class myarray {
    int x[5] = { 1, 2, 3, 4, 5 };
public:
    //put breakpoint anywhere inside a function of this class:
    class iterator {
        int* p0, *p;
    public:
        iterator(int* _p) : p(_p), p0(_p) {}
        int& operator*() { return *p; }
        iterator& operator++() { ++p; std::cout << "position "<< position() << std::endl; return *this; }
        size_t position() { return p - p0; }
        bool operator!=(const iterator& other) const { return p != other.p; }
    };
    iterator begin() { return iterator(x); }
    iterator end() { return iterator(x + 5); }

};
....
In the nested class iterator you can give as much control over iterations as you like.

Now this will be the output:

[enter image description here]
For different cases any other container specific iterator and position calculation can be implemented.

(https://i.sstatic.net/8M2bVLvT.png)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.