0

In C++, when referencing identifiers declared in the standard namespace, these identifiers need to be qualified with "std::" (name of namespace + scope resolution operator). However, my program compiles without error even if I don't qualify identifiers of functions.

For instance, the following code compiles without error:

#include <iostream>
#include <string>

int main() {
    std::string s;
    std::getline(std::cin, s); // works.
    getline(std::cin, s); // also works. Why?
}

Running g++ --version outputs the following:

Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Why is the second last line in the sample code above correct? Does it work for all compilers/environments?

Cynicrom
  • 29
  • 4
  • 4
    [ADL](https://en.cppreference.com/w/cpp/language/adl) – songyuanyao Jul 21 '23 at 07:39
  • If not for ADL, you might have to do `std::string a{"alpha"}, b{"beta"}; auto s = std::operator+(a, b);` which some people might find inconvenient. – Eljay Jul 21 '23 at 12:02

0 Answers0