简体   繁体   English

为什么没有命名空间限定符的std :: generate是可访问的?

[英]Why is std::generate accessible without namespace qualifier?

Is it normal that this compiles fine? 编译好是正常的吗?

#include <vector>
#include <algorithm>

int main()
{
    std::vector<int> buf;
    generate(buf.begin(), buf.end(), []{ return 0; });
}

(Note the missing std:: in front of generate() ) (注意在generate()前面缺少的std::

Is this behavior documented somewhere? 这种行为是否记录在某处? Or did I stumble across a compiler or library bug? 或者我偶然发现了编译器或库错误? Which in my case would be GCC 5.3.0 and Clang 3.8.0 on Linux; 在我的例子中,在Linux上是GCC 5.3.0和Clang 3.8.0; both use libstdc++, so maybe library bug? 两者都使用libstdc ++,所以也许库bug?

This is allowed, essentially because the arguments to generate are in std . 这是允许的,主要是因为generate的参数是std

Code like 代码就像

namespace Foo
{
    struct B{};
    void foo(const B&);
}

int main()
{
    Foo::B b; /*Requires Foo::*/
    foo(b); /*Does not require Foo:: as that is gleaned from the argument*/
}

is acceptable for similar reasons. 因类似原因可以接受。 We call it argument dependent lookup . 我们称之为参数依赖查找 See https://en.wikipedia.org/wiki/Argument-dependent_name_lookup 请参阅https://en.wikipedia.org/wiki/Argument-dependent_name_lookup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM