C ++ getline () does not require a namespace declaration

Why getline () from the header is a string in local scope and can be used:

#include <iostream> #include <string> int main() { std::string str; getline(std::cin, str); std::cout << str << "\n"; return 0; } 

This works with gcc. But why? It is defined in the string header, so I would need to use std :: getline () instead of getline ().

+6
source share
1 answer

You experience an argument-dependent search (ADL, also called Koenig search). Since one or more arguments is a type defined in the std , it looks for a function in the std in addition to wherever it is. I point you to a Stephan T. Lavavej video to learn more about this and find the name as a whole.

+8
source

All Articles