开发者

STL algorithm function name resolution

开发者 https://www.devze.com 2023-03-06 19:14 出处:网络
I\'d expect in the example bellow compiler will fail to compile the code, since it doesn\'t know what is \"find()\", which defined in std namespace in algorithm header.

I'd expect in the example bellow compiler will fail to compile the code, since it doesn't know what is "find()", which defined in std namespace in algorithm header.

However this code compiles on RHEL 5.3 with gcc 4.1.2.

What do I miss?

#include <string>    
#include <algorithm>

int main()
{
    std::string s;
    find(s.begin(), s.end(), 'a');  // should not c开发者_StackOverflow社区ompile
}


This works due to Argument Dependent Lookup. The function-template is searched in the namespace of the arguments types. In this case, the arguments are std::string::iterator, so the function is searched in the namespace std.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号