书中提到,如果swap的缺省实现对你的class或class template 效率不足,试着做一下事情
最后,如果你调用swap, 请确定包含一个using声明式,以便让std:swap在你的成员函数内曝光课件,然后不加任何namesapce修饰符,赤裸裸调用swap。
我的疑问是有了第二点,还为什么要第三点? 因为根据augument-dependent lookup,就能找到class 或tempalte命名空间的swap,然后就会调用之。为什么还要有第三点?感觉没必要啊。
大家讲道理2017-04-17 11:08:58
This is also described in the book:
template<typename T>
void doSomething(T& obj1, T& obj2)
{
using std::swap;
swap(obj1, obj2);
}
This way of writing can find the non-member function specialized in the class through ADL (argument-dependent lookup).
2. In some cases (actually many cases), it will be inappropriately written as the following code:
template<typename T>
void doSomething(T& obj1, T& obj2)
{
std::swap(obj1, obj2);
}
If specialization in std namespace is not provided (Item 3), the above code will only use unspecialized std::swap, thus causing performance problems.
In general, the third point is a way to avoid traps.