如何为我的班级提供交换功能?

pic*_*c11 82 c++ algorithm swap stl

启用swapSTL算法的正确方法是什么?

1)会员swap.是否std::swap使用SFINAE技巧来使用该成员swap.

2)自由站立swap在同一名称空间中.

3)部分专业化std::swap.

4)以上所有.

谢谢.

编辑:看起来我没有清楚地说出我的问题.基本上,我有一个模板类,我需要STL algos来使用我为该类编写的(高效)交换方法.

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),因为它更适合.

  • 你的(1)和问题的(1)并没有真正对齐,除非我误读了什么.仍然是+1 (8认同)
  • 这个答案在技术上是正确的,但为了清晰起见,**非常需要编辑.OP的(1)不是正确答案,因为在这个答案中过于快速的阅读似乎错误地指出了. (4认同)
  • 第一段代码中的评论具有误导性.`使用std :: swap;`不启用ADL,它只允许编译器找到`std :: swap`,如果ADL没有找到正确的重载. (2认同)