From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18646 invoked by alias); 29 Mar 2011 16:42:06 -0000 Received: (qmail 18455 invoked by uid 22791); 29 Mar 2011 16:42:04 -0000 X-SWARE-Spam-Status: No, hits=-2.2 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-wy0-f175.google.com (HELO mail-wy0-f175.google.com) (74.125.82.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 29 Mar 2011 16:41:59 +0000 Received: by wye20 with SMTP id 20so341075wye.20 for ; Tue, 29 Mar 2011 09:41:58 -0700 (PDT) MIME-Version: 1.0 Received: by 10.216.144.134 with SMTP id n6mr8308wej.27.1301416868848; Tue, 29 Mar 2011 09:41:08 -0700 (PDT) Received: by 10.216.78.132 with HTTP; Tue, 29 Mar 2011 09:41:08 -0700 (PDT) In-Reply-To: References: <4D8A2403.5050708@redhat.com> <4D90A209.2020508@redhat.com> Date: Tue, 29 Mar 2011 16:55:00 -0000 Message-ID: Subject: Re: [C++0x] Range-based for statements and ADL From: Rodrigo Rivas To: Gabriel Dos Reis Cc: Jonathan Wakely , Jason Merrill , gcc-patches@gcc.gnu.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org X-SW-Source: 2011-03/txt/msg02009.txt.bz2 On Tue, Mar 29, 2011 at 5:46 PM, Gabriel Dos Reis wrote: > or "new-style for loop"? Well, that is what they are called in Java, isn't it? And the syntax is just the same, so it would make kind of sense. But in the C++0x draft and the GCC docs it is almost always called "range-based for loops, so I think it is preferred. Talking of this... I tried the simplest wrong range-based for, with my latest patch: int main() { for (int x : 0) ; } And that is what I got: test.cpp: In function =E2=80=98int main()=E2=80=99: test.cpp:3:18: error: =E2=80=98begin=E2=80=99 was not declared in this scope test.cpp:3:18: error: =E2=80=98end=E2=80=99 was not declared in this scope That's ok. But now, if I add #include I get: test.cpp: In function =E2=80=98int main()=E2=80=99: test.cpp:4:18: error: no matching function for call to =E2=80=98begin(int&)= =E2=80=99 test.cpp:4:18: note: candidates are: /home/rodrigo/local/gcc/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include= /c++/4.7.0/bits/range_access.h:87:5: note: template _Tp* std::begin(_Tp (&)[_Nm]) /home/rodrigo/local/gcc/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include= /c++/4.7.0/bits/range_access.h:58:63: note: template decltype (__cont->begin()) std::begin(const _Container&) /home/rodrigo/local/gcc/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include= /c++/4.7.0/bits/range_access.h:48:57: note: template decltype (__cont->begin()) std::begin(_Container&) /home/rodrigo/local/gcc/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include= /c++/4.7.0/initializer_list:86:38: note: template constexpr const _Tp* std::begin(std::initializer_list<_Tp>) test.cpp:4:18: error: no matching function for call to =E2=80=98end(int&)= =E2=80=99 test.cpp:4:18: note: candidates are: /home/rodrigo/local/gcc/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include= /c++/4.7.0/bits/range_access.h:97:5: note: template _Tp* std::end(_Tp (&)[_Nm]) /home/rodrigo/local/gcc/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include= /c++/4.7.0/bits/range_access.h:78:59: note: template decltype (__cont->end()) std::end(const _Container&) /home/rodrigo/local/gcc/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include= /c++/4.7.0/bits/range_access.h:68:53: note: template decltype (__cont->end()) std::end(_Container&) /home/rodrigo/local/gcc/lib/gcc/i686-pc-linux-gnu/4.7.0/../../../../include= /c++/4.7.0/initializer_list:96:36: note: template constexpr const _Tp* std::end(std::initializer_list<_Tp>) Although the error messages are technically correct, I find them too verbose and probably misleading to the novice: "'begin' not declared? I'm not using 'begin', I'm using 'for'). Note also that the error could be corrected adding member functions begin/end (if the range were of class type), but that is not suggested in the error message. Maybe a simpler error is better. such as: "expression is not valid as range in a range-based % loop because it lacks begin/end members or begin(int&) and end(int&) overloads." (I leave the wording to someone more skilled with English). Regards. -- Rodrigo