The "member interpretation" refers to begin_expr
and end_expr
using members of the iterated type in contrast to using plain offsets for arrays or begin
and end
free functions.
The array interpretation is off the table, because it is only used for arrays. Next consider that there is std::begin
and std::end
:
Custom overloads of begin may be provided for classes and enumerations that do not expose a suitable begin() member function, yet can be iterated.
Now consider this example:
#include <iostream>
#include <vector>
class meow {
enum { begin = 1, end = 2};
public:
std::vector<int> data;
};
// non-const iterators
auto begin(meow& m){ return m.data.begin(); }
auto end(meow& m) { return m.data.end(); }
// const iterators
auto begin(const meow& m){ return m.data.begin(); }
auto end(const meow& m) { return m.data.end(); }
int main() {
meow m;
for (const auto& e : m) {}
}
We want to iterate the meow
s data
. But it does not work. The member interpratation is choosen, even though meow::begin
and mewo::end
are private and the begin
and end
functions could be used. Hence the error:
<source>: In function 'int main()':
<source>:17:26: error: 'meow::<unnamed enum> begin' is private within this context
for (const auto& e : m) {}
^
<source>:5:12: note: declared private here
enum { begin = 1, end = 2};
^~~~~
<source>:17:26: error: 'begin' cannot be used as a function
for (const auto& e : m) {}
^
<source>:17:26: error: 'meow::<unnamed enum> end' is private within this context
<source>:5:23: note: declared private here
enum { begin = 1, end = 2};
^~~
<source>:17:26: error: 'end' cannot be used as a function
for (const auto& e : m) {}
^
The example works fine when we remove the private enum:
#include <iostream>
#include <vector>
class meow {
//enum { begin = 1, end = 2};
public:
std::vector<int> data;
};
// non-const iterators
auto begin(meow& m){ return m.data.begin(); }
auto end(meow& m) { return m.data.end(); }
// const iterators
auto begin(const meow& m){ return m.data.begin(); }
auto end(const meow& m) { return m.data.end(); }
int main() {
meow m;
for (const auto& e : m) {}
}
Live Demo
begin
andend
insidemeow
and since those aren't functions that return iterators, the range based for loop can't work. – Antitank