我希望在下面的示例中编译器将无法编译代码,因为它 不知道什么是“find()”,它定义在 algorithm header 中的 std 命名空间中。
但是,此代码在 RHEL 5.3 上使用 gcc 4.1.2 编译。
我错过了什么?
#include <string>
#include <algorithm>
int main()
{
std::string s;
find(s.begin(), s.end(), 'a'); // should not compile
}
最佳答案
这是由于参数依赖查找而起作用的。在参数类型的命名空间中搜索函数模板。在这种情况下,参数是 std::string::iterator
,因此在命名空间 std
中搜索函数。
关于c++ - STL算法函数名解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6055755/