4

Let's consider such code:

#include <vector>

int main() {
    vector<int> v = {1, 2, 3};
    return 0;
}

GCC 6.3 doesn't compile it with error: ‘vector’ was not declared in this scope - great, I'd expect that. I need to write std::vector to compile, good.

However, the following does compile:

#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v = {1, 2, 3};
    int c = count(v.begin(), v.end(), 2);
    return 0;
}

It looks like count is imported into global namespace. I also tried find - the same.

Why #include <algorithm> imports names into global namespace? Which C++ Standard Library headers do this and which not?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • 3
    This is called ADL: https://stackoverflow.com/questions/8111677/what-is-argument-dependent-lookup-aka-adl-or-koenig-lookup –  Jul 03 '18 at 18:42
  • 1
    Btw, `std::vector<>::iterator` might simply be pointer without ADL. – Jarod42 Jul 03 '18 at 18:44

0 Answers0