Xeo*_*Xeo 92
1)是正确使用的swap.当您编写"库"代码并希望启用ADL(依赖于参数的查找)时,以这种方式编写swap.此外,这与SFINAE无关.
// some algorithm in your code
template<class T>
void foo(T& lhs, T& rhs){
using std::swap; // enable 'std::swap' to be found
// if no other 'swap' is found through ADL
// some code ...
swap(lhs, rhs); // unqualified call, uses ADL and finds a fitting 'swap'
// or falls back on 'std::swap'
// more code ...
}
Run Code Online (Sandbox Code Playgroud)
2)是swap为您的班级提供功能的正确方法.
namespace Foo{
class Bar{}; // dummy
void swap(Bar& lhs, Bar& rhs){
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
如果swap现在使用如1)所示,将找到您的功能.此外,如果您绝对需要,您可以将该功能设为朋友,或者提供swap由免费功能调用的成员:
// version 1
class Bar{
public:
friend void swap(Bar& lhs, Bar& rhs){
// ....
}
};
// version 2
class Bar{
public:
void swap(Bar& other){
// ...
}
};
void swap(Bar& lhs, Bar& rhs){
lhs.swap(rhs);
}
Run Code Online (Sandbox Code Playgroud)
3)你的意思是明确的专业化.部分仍然是其他东西,也不可能用于函数,只有结构/类.因此,由于您不能专门std::swap用于模板类,因此必须在命名空间中提供自由函数.如果我可以这样说,那也不错.现在,也可以进行显式特化,但通常您不希望专门化函数模板:
namespace std
{ // only allowed to extend namespace std with specializations
template<> // specialization
void swap<Bar>(Bar& lhs, Bar& rhs){
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
4)否,因为1)与2)和3)不同.此外,同时拥有2)和3)将导致总是选择2),因为它更适合.