loops - get the index of a C++ foreach item in a debugger - Stack Overflow

Lets assume the code doesn't need the index, but something interesting happened during debugging a

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743747998a4500368.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信