This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++0x] Range-based for statements and ADL


Hello all.

I've just read the paper titled "Range-based for statements and ADL"
by Jonathan Wakely and Bjarne Stroustrup at:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3257.pdf

The point of this article is that the argument dependent lookup in the
range-for specification will make some trouble in the real world. E.g:
#include <vector>
namespace n
{
    struct X { void begin(); };
    struct Y { void begin(); };
    template<typename T> void begin(T& t) { t.begin(); }
}
int main()
{
    std::vector<n::X> v;
    for (auto i : v)  // error: call to begin is ambiguous: std::begin
or n::begin?
    {
        //...
    }
}

I've implemented Option 5 from the paper (which is the coolest, IMHO):
 - [...], if _rangeT has a member begin or a member end, begin-expr
and end-expr are __range.begin() and __range.end(), respectively
 - otherwise, begin-expr and end-expr are begin(__range) and
end(__range), respectively, where begin and end are looked up with
argument-dependent lookup [...]

I'm not sure about what should happen if _rangeT has a member begin
but not a member end. I think that there are just two sensible
options:
 * Use them only if both members, begin and end, exist.
 * Look for them independently. This is what my patch does.

Also, if the member begin/end is not accessible or not callable, a
compiler error will follow immediately (this is as expected).

I'll appreciate any comments.

Best regards
--
Rodrigo

Attachment: range_for_new_lookup.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]