Re: std::count leaked outside namespace std?
On Wed, Apr 24, 2013 at 5:25 AM, Nathan Ridge wrote: >> Here's a simple program: >> >> #include >> #include >> >> int main() >> { >> std::vector vec; >> count(vec.begin(), vec.end(), 0); // shouldn't this be std::count ? >> } >> >> The above compiles successfully, but I think it shouldn't. I expect a >> message like "error: `count` not declared in scope" because I meant to >> say std::count. Isn't this a bug, or am I missing something? > > It is not a bug. std::count is being found by argument-dependent lookup [1]. > > Regards, > Nate > > [1] http://en.wikipedia.org/wiki/Argument-dependent_name_lookup For completeness (or something closer to it): the code is non-portable, as the C++ Standard doesn't say in which namespace the type named by std::vector::iterator is defined. It's commonly either (a) in namespace std, in which case the code above compiles, or (b) a raw pointer, in which case the code above does not compile. It's also entirely possible that switching from a debug build to a non-debug one might change from (a) to (b) with the same compiler and library. -- James
RE: std::count leaked outside namespace std?
> Here's a simple program: > > #include > #include > > int main() > { > std::vector vec; > count(vec.begin(), vec.end(), 0); // shouldn't this be std::count ? > } > > The above compiles successfully, but I think it shouldn't. I expect a > message like "error: `count` not declared in scope" because I meant to > say std::count. Isn't this a bug, or am I missing something? It is not a bug. std::count is being found by argument-dependent lookup [1]. Regards, Nate [1] http://en.wikipedia.org/wiki/Argument-dependent_name_lookup
Re: std::count leaked outside namespace std?
Thanks Paolo, ADL is news to me. On 24 April 2013 01:43, Paolo Carlini wrote: > You are: https://en.wikipedia.org/wiki/Argument-dependent_name_lookup > > Paolo. >
Re: std::count leaked outside namespace std?
On 04/23/2013 11:26 PM, bd satish wrote: Hi, Here's a simple program: #include #include int main() { std::vector vec; count(vec.begin(), vec.end(), 0); // shouldn't this be std::count ? } The above compiles successfully, but I think it shouldn't. I expect a message like "error: `count` not declared in scope" because I meant to say std::count. Isn't this a bug, or am I missing something? You are: https://en.wikipedia.org/wiki/Argument-dependent_name_lookup Paolo.