简体   繁体   English

C ++ getline()不需要命名空间声明

[英]C++ getline() doesn't require namespace declaration

Why is getline() from header string in local scope and can be used: 为什么getline()来自本地范围内的头字符串并且可以使用:

#include <iostream>
#include <string>

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

That works with gcc. 这适用于gcc。 But why? 但为什么? It is defined in header string , which should required me to use std::getline() instead of getline() . 它在头字符串中定义,它应该要求我使用std :: getline()而不是getline()

You're experiencing Argument Dependent Lookup (ADL, and also referred to as Koenig Lookup). 您正在体验Argument Dependent Lookup(ADL,也称为Koenig Lookup)。 Since one or more of the arguments is a type defined in the std namespace, it searches for the function in the std namespace in addition to wherever else it would search. 由于一个或多个参数是std命名空间中定义的类型,因此除了搜索其他任何地方外,它还会在std命名空间中搜索该函数。 I point you to Stephan T. Lavavej's video to learn more about it and name lookup in general. 我指向Stephan T. Lavavej的视频,以了解更多关于它和名称查询的信息。

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

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