在我最近写的代码中,我注意到了一种奇怪的行为.
当我使用make_pair第一个参数为a时std::pair,make_pair在命名空间中变得"神奇地"可用(我不必使用std::限定符)
#include <iostream>
int main()
{
int i1 = 2; int i2 = 10; int i3 = 0;
// constructing a pair using std::make_pair, everything's okay
std::pair<int,int> key = std::make_pair(i1, i2);
// here, why is make_pair suddenly magically available without the
// std:: namespace qualifier?
// At the same time, using ::make_pair yields and error
// (make_pair has not declared...)
std::pair<std::pair<int,int>, int> mypair = make_pair(key, i3);
std::cout << mypair.first.first << "\n";
std::cout << mypair.first.second << "\n";
std::cout << mypair.second << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译就好(带-Wall and -pedantic-errors)和输出:
2
10
0
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我查看了cppreference并没有发现任何关于这种行为的提示是正确的.我错过了什么吗?
仅供参考,我正在使用gcc 4.6.3
Bar*_*icz 22
这是一个鲜为人知的C++特性,因为@jrok非常快速地指出,Koenig Lookup,或现代C++ 1),ADL(Argument-Dependent Lookup).它的作用基本上是在你想要调用的函数的参数的名称空间中搜索(make_pair在这个例子中).触发ADL的论点显然是std::pair.
1)命名已经改变,虽然很多人都知道第一个任期
也许值得一提的是,ADL对于一种特定类型的函数非常重要:运算符.如果不是这样,即使是微不足道的C++也不可能"你好,世界!" 工作,因为这:
std::cout << "Hello, world!";
Run Code Online (Sandbox Code Playgroud)
必须写成这样:
std::operator<< (std::cout, "Hello, world!");
Run Code Online (Sandbox Code Playgroud)
感谢ADL,<<正确解析为std名称空间.
参考文献:
| 归档时间: |
|
| 查看次数: |
476 次 |
| 最近记录: |