C++ getline() 不需要命名空间声明
C++ getline() doesn't require namespace declaration
为什么getline() from header string在局部作用域内并且可以使用:
#include <iostream>
#include <string>
int main() {
std::string str;
getline(std::cin, str);
std::cout << str << "n";
return 0;
}
可以在gcc中使用。但是为什么呢?它在header string中定义,这应该要求我使用std::getline()而不是getline()。
您正在经历参数依赖查找(ADL,也称为Koenig查找)。由于一个或多个参数是在std
名称空间中定义的类型,因此除了在其他地方搜索之外,它还在std
名称空间中搜索该函数。我建议你去看Stephan T. Lavavej的视频,了解更多关于它和名字查找的知识。
相关文章:
- 在命名空间 c++ 中正确声明 extern 变量
- 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见
- 转发声明在命名空间中不起作用的替代方法
- C++ 17 个友元函数声明和内联命名空间
- 为什么 gcc 无法从其前向声明中检测到友元类命名空间?
- 声明"使用命名空间 C;"对于证明 [namespace.udir]/3 中的示例中显示的结果至关重
- 命名空间内C++变量声明
- 在命名空间中声明变量
- C++ 没有在命名空间外部声明的类主体的类
- 如何在命名空间中声明外部全局,然后定义它?
- 在 c++ 中在运行时声明对命名空间的引用
- 前向声明和全局命名空间声明
- 将命名空间声明为类的友元
- 冗余命名空间声明向量
- c++使用命名空间声明
- 是否有办法配置clang-format使嵌套的命名空间声明保持在同一行
- gSOAP WSSE报头缺少xmlns命名空间声明
- C++ getline() 不需要命名空间声明
- C++命名空间声明和定义
- 使用嵌套命名空间声明"using namespace"时出错 ( "namespace xxx::yyy not allowed in using-declaration" )