[Bug c++/71010] error: 'begin' was not declared in this scope
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Mon May 9 09:54:00 GMT 2016
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71010
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
No, it's not a bug. Range-based 'for' is not supposed to find std::begin and
std::end unless std is an associated namespace.
Furthermore, your code has undefined behaviour, it is forbidden to add your own
functions to namespace std. The correct way to do it is to write the begin/end
overloads in the same namespace as your type (in this case that's the global
namespace).
class X
{
int m_x[5] = { 0, 1, 2, 3, 4 };
public:
int* first() { return &m_x[0]; }
int* last() { return &m_x[4]; }
};
int* begin(X& x) { return x.first(); }
int* end(X& x) { return x.last(); }
This works with all versions of GCC that support C++11.
More information about the Gcc-bugs
mailing list