[Bug c++/71010] error: 'begin' was not declared in this scope
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71010 --- Comment #5 from Jonathan Wakely --- (In reply to Ubikovich from comment #3) > But since C++14 cbegin invoke std::begin: > > // http://en.cppreference.com/w/cpp/iterator/begin > template< class C > > constexpr auto cbegin( const C& c ) -> decltype(std::begin(c)); > (3) (since C++14) > > Then in what namespace should be user-defined begin/end? I already explained how to do it correctly, please read comment 1 again. (In reply to Andrew Pinski from comment #4) > In the same as the class so ADR (argument dependent resolution) can find it. N.B. that should be ADL (argument dependent *lookup*).
[Bug c++/71010] error: 'begin' was not declared in this scope
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71010 Andrew Pinski changed: What|Removed |Added Resolution|FIXED |INVALID --- Comment #4 from Andrew Pinski --- (In reply to Ubikovich from comment #3) > But since C++14 cbegin invoke std::begin: > > // http://en.cppreference.com/w/cpp/iterator/begin > template< class C > > constexpr auto cbegin( const C& c ) -> decltype(std::begin(c)); > (3) (since C++14) > > Then in what namespace should be user-defined begin/end? In the same as the class so ADR (argument dependent resolution) can find it.
[Bug c++/71010] error: 'begin' was not declared in this scope
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71010 Ubikovich changed: What|Removed |Added Resolution|INVALID |FIXED --- Comment #3 from Ubikovich --- But since C++14 cbegin invoke std::begin: // http://en.cppreference.com/w/cpp/iterator/begin template< class C > constexpr auto cbegin( const C& c ) -> decltype(std::begin(c)); (3) (since C++14) Then in what namespace should be user-defined begin/end?
[Bug c++/71010] error: 'begin' was not declared in this scope
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71010 Manuel López-Ibáñez changed: What|Removed |Added CC||manu at gcc dot gnu.org --- Comment #2 from Manuel López-Ibáñez --- (In reply to Jonathan Wakely from comment #1) 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). I actually did not know that. Would it be possible to warn about this? I guess libstdc++ files are system-headers and can avoid the warning.
[Bug c++/71010] error: 'begin' was not declared in this scope
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71010 Jonathan Wakely changed: What|Removed |Added Status|UNCONFIRMED |RESOLVED Resolution|--- |INVALID --- Comment #1 from Jonathan Wakely --- 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.