Recently seen on a mailing list.

Purportedly a fairly common idiom in defensive code:

for (auto &value: a_vector) {
if (&value < a_vector.begin() || a.end() <= &value) {
abort();
}
opaque_function();
}

Thoughts?

Follow

@DanielaKEngert this doesn't seem to make any sense.
The `for (value : container)` syntax puts the element 's value into `value`.
In your case as a reference.
Now they check the address of the value against the iterator for the container.
I wonder how implicit type matching should happen here. Might probably work with arrays and a very... crude implementation of iterators.
Remains the question as to why one would choose to do such a check?
Looks a bit like one knows they're doing it wrong somewhere else and try and and work around an alleged bug where the problem is somewhere else.
Well, when I was young, I too went down each and every rabbit hole (or Holzweg). Reviews or pairing could heal this ;-)

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.