ADL
例子:
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector v{1, 3, 5}; // 模板类参数推导:T=int
std::cout << std::accumulate(begin(v), end(v), 0);
// ^^ ^^ ^^ 都是ADL
}
使用 begin、end、swap 等函数模板时,先引入其名字到当前空间中,然后再调用,有助于 ADL 查找到性能更优的函数实现:
{
using std::swap;
swap(A, B); // 假设 A 和 B 是已经定义的同类型变量
}
博文 Should I use the two-step in non-generic code? 认为在非泛型代码中不必使用 using std::swap,直接使用 x.swap(y) 或者 std::swap(如果是内置类型或者 std 名字空间中的类型)即可。