Show example ples
Imagine you have a structure like this
struct Bar {
Bar * begin() { return this; }
Bar *end() { return this; }
};
Using it an a for-range loop works fine, since it's iterable. If you want to get the start iterator std::begin works as well.
—————————————-
Now we got a second structure
struct Foo {
friend Foo* begin(Foo& f) {
return &f;
}
friend Foo* end(Foo& f) {
return &f;
}
};
Using a for-range loop works fine again
void foo() {
for (auto && f : Foo{}) {}
}
Using std::begin fails with
error: no matching function for call to 'begin(Foo&)'
—————————————————-
This is not what we want, for example, we might want to implement a enumerate that basically does the same as python's enumerate, e.g.
for(auto&& [idx, val] : enumerate(Foo{})) {}
So we want to call the friend function for Foo but std::begin Bar, ADL is the solution by doing something like
template <class T> auto beg(T&& t)
{
using std::begin;
return begin(t);
}