Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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())

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; both use libstdc++, so maybe library bug?

like image 496
Nikos C. Avatar asked Apr 04 '16 14:04

Nikos C.


1 Answers

This is allowed, essentially because the arguments to generate are in 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

like image 104
Bathsheba Avatar answered Nov 12 '22 20:11

Bathsheba