[cfe-dev] Range for loop and custom container

Richard Smith richard at metafoo.co.uk
Thu Jun 4 13:49:01 PDT 2015


On Thu, Jun 4, 2015 at 1:09 PM, János Hack <hjjohny at gmail.com> wrote:

> Hi all,
> Please consider this example:
>
>
> template<typename T>
> struct vec{};
>
> namespace std
> {
>   template<typename T>
>   T* begin(vec<T>& a)
>   {
>     return 0;
>   }
>
>   template<typename T>
>   T* begin(const vec<T>& a)
>   {
>     return 0;
>   }
>
>   template<typename T>
>   T* end(vec<T>& a)
>   {
>     return 0;
>   }
>
>   template<typename T>
>   T* end(const vec<T>& a)
>   {
>     return 0;
>   }
> }
>
> using namespace std;
>
> int main()
> {
>   vec<char> vecDef;
>   for(auto it:vecDef);
>
>   return 0;
> }
>
> clang compile: clang++ -std=c++11 rangedfor.cpp (the code above)
> result:rangedfor.cpp:37:14: error invalide range expression of type
> 'vec<char>'; no viable 'begin' function available
>
> i already read
> http://comments.gmane.org/gmane.comp.compilers.clang.devel/31712
> <https://urldefense.proofpoint.com/v2/url?u=http-3A__comments.gmane.org_gmane.comp.compilers.clang.devel_31712&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=ImzhkWDMFEMiWBqnwuyC-isY3Kc5XNTD5dRy8mg7X7M&s=un4RlsLD0nXrDD6ZtmK66JmSdLCsq7gnYrPB2kk46WM&e=>
> but in that, he creates a new begin and her,e i create an overload and this
> is enabled:
>
> http://en.cppreference.com/w/cpp/iterator/begin
> <https://urldefense.proofpoint.com/v2/url?u=http-3A__en.cppreference.com_w_cpp_iterator_begin&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=ImzhkWDMFEMiWBqnwuyC-isY3Kc5XNTD5dRy8mg7X7M&s=v3nzN8N0CSO2-EMbtjLy1FyKt25ZrRkBTqK08lWZHY4&e=>
> see  User-defined overloads part
>
> The question is: am i intepret this wrong or this is a bug or what? the
> g++ , and visual c++ compile this code without a question.
>

That's a bug in those other compilers. You're supposed to put the
non-member begin/end functions in the same namespace as your container.


> clang version: 26 May 2015   3.6.1
> release windows, but in linux the same problem stands
>
> Thanks János
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150604/d031da52/attachment.html>


More information about the cfe-dev mailing list