boost::swap

boost :: swap is a standard library of std :: swap enhancement and generalization, provides a convenient way to exchange value of the two variables.

To use must contain header file: #include <boost / swap.hpp>

Principle
c98 standard std :: swap ():

  template<typename T>
  void swap(T& a,T& b)
  {
    T tmp(a);
       a = b;
       b = temp;      
      }              

In the swap standard library, the object exchange requirements must be configured to copy and the copy assignment, inefficient, require a copy constructor and two assignment. c ++ 11 using a transfer standard semantic std :: swap () is optimized:

template<typename T>
  void swap(T& a,T& b)
  {
      T tmp = std::move(a);         //把a'偷'到tmp
      a = std::move(b);             //把b'偷'到a
      b = std::move(tmp);               //把tmp'偷'到b
  }

But not all classes implement their own state transition structure and function assignment, and compiler support is also an issue that must be considered, so for us to write the class, it is best to optimize the swap () to improve efficiency.
There are two solutions: one is the direct use of function overloading, write swap function with the same name, calls the member function of university exchange function, do not use swap the standard library, the second is the use of ADL (parameter dependent lookup) Find template specialization of std :: swap ().
boost :: Find whether the type T for std :: swap () template specialization or find specialized swap by ADL (), if the call fails to find degenerate into std :: swap (); due boost :: swap () and std :: swap () with the same name, so we can not open the boost namespace by using, should call it a boost namespace defined manner.

  • Switching array
    boost :: swap () contents of the two arrays can be exchanged directly, but requires two arrays involved in the exchange must be the same length.
    • int al[10],a2[12];    //两个不通长度的数组
      boost::swap(al,a2) //编译错误
  • Specialization of std :: swap () - a simple three-dimensional space nodes exchange as an example, the use of template specialization ways to use boost :: swap ():

    #include <boost/swap.hpp>
    #include <ostream>
    using namespace std;
    class point
    {
        int x,y,z;
        public:
        explict point(int a=0,int b=0,int c=0): x(a),y(b),z(c) {}
    
        void swap(point &p)
        {
            std::swap(x,p.x);
            std::swap(y,p.y);
            std::swap(z,p.z);
            cout<<"inner swap"<<endl
        }
    };
    namespace std{
        template<>
        void swap(point &x,point &y)
        { x.swap(y); }
    }
    int main()
    {
        point a(1,2,3),b(4,5,6);
        cout<<"std::swap"<<endl;
        std::swap(a,b);
        cout<<"boost::swap"<<endl;
        boost::swap(a,b);
    }
    //运行结果:
    std::swap
    inner swap
    boost::swap
    inner swap
    由于我们在名字空间特化了std::swap,因此boost::swap和std::swap调用结果是相同的,都使用了特化后的swap函数。
  • Specialized ADL can be found in the swap
    only change is to achieve global domain swap function:

    #include <boost/swap.hpp>
    #include <ostream>
    using namespace std;
    class point
    {
        int x,y,z;
        public:
        explict point(int a=0,int b=0,int c=0): x(a),y(b),z(c) {}
    
        void swap(point &p)
        {
            std::swap(x,p.x);
            std::swap(y,p.y);
            std::swap(z,p.z);
            cout<<"inner swap"<<endl
        }
    };
    void swap(point & x,point& y)
        {
            x.swap(y);
        }
    int main()
    {
        point a(1,2,3),b(4,5,6);
        cout<<"std::swap"<<endl;
        std::swap(a,b);
        cout<<"boost::swap"<<endl;
        boost::swap(a,b);
    }
    //运行结果:
    std::swap
    boost::swap
    inner swap
    与之前的特化std::swap不同,std::swap使用了标准的交换操作,而boost::swap通过ADL规则找到全局名字空间的特化交换函数,实现高效的交换。

Guess you like

Origin www.cnblogs.com/kloseer/p/12069506.html