Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Failure to understand ADL

12 views
Skip to first unread message

Alf P. Steinbach

unread,
Nov 20, 2016, 8:20:31 AM11/20/16
to
On 20.11.2016 13:46, Stefan Ram wrote:
> I have written this program, which uses ADL.
> Observe that there is no »::std« before »binary_search«.
>
> #include <algorithm>
> #include <initializer_list>
> #include <iostream>
> #include <ostream>
> #include <string>
> #include <iterator>
> int main()
> { ::std::string str{ "abcdefghijklmnopqrstuvwxyz" };
> ::std::cout << binary_search( cbegin( str ), cend( str ), 'c' )<< '\n';
> ::std::cout << binary_search( cbegin( str ), cend( str ), '#' )<< '\n'; }
>
> This worked. Next program I wrote:
> Observe that there is »::std« before »for_each«.
>
> main.cpp
> #include <algorithm>
> #include <initializer_list>
> #include <iterator>
> #include <ostream>
> #include <iostream>
> void print( char const c ){ ::std::cout << c << '\n'; }
> int main()
> { ::std::initializer_list< char > l { 'a', 'b', 'c' };
> ::std::for_each( cbegin( l ), cend( l ), print ); }
>
> I seem to need the »::std« in front of »for_each« in
> this case. Why?

With a `std::initializer_list` the begin and end iterators are raw pointers.

This can also happen with other containers, depending on the standard
library implementation, but with `std::initializer_list` it's guaranteed.

Here's reliable documentation: <url:
http://en.cppreference.com/w/cpp/utility/initializer_list>.


Cheers & hth.,

- Alf


0 new messages