C++ boost::swap.hpp::swap














































C++ boost::swap.hpp::swap



Description:
The function boost::swap can be used to swap the values of two variables. The specialized swap function is selected using the argument dependent lookup but if no specialized swap function is available than, std::swap is used.

Rationale:
The std::swap function requires the elements that needs to be swapped must be assignable and copy constructive. This basic swap function is implemented by two assignments and one copy construction. This is sometimes restrictive and slow.Template specialization of std::swap can be used for every type that requires a specialized swap.

Many boost libraries provide specialized swap functions in their own namespaces. boost::swap allows swapping of built-in arrays. However std::swap did not do so.

 Exception Safety:
boost::swap provides same exception guarantee as of std::swap function, with on exception: for an array of type T[n], for n>1 and the std::swap function provides strong exception guarantee, instead boost::swap provides basic exception guarantee.

 Some Requirements:
  1. T must be assignable and copy constructive.
  2. A function with the signature swap(T& , T&) is available via argument dependent lookup
  3. Template specialization of std::swap exists for T
  4. T is a built-in array of swap elements

Implementation:
#include <boost/swap.hpp>
#include <boost/array.hpp>
#include <iostream>

using namespace std;

int main()
{
  char c1 = 'x';
  char c2 = 'y';
  cout<< "The characters before swapping :"<<"c1 : "<<c1<<", c2 : "<<c2<<endl;
  boost::swap(c1, c2);  //This call swaps the characters

  cout<< "The characters after swapping :"<<"c1 : "<<c1<<", c2 : "<<c2<<endl;

  boost::array<int, 1> a1{{1}};   //Array with single element
  boost::array<int, 1> a2{{2}};
  cout<< "The array before swapping :"<< a1[0] <<" "<< a2[0] << '\n';
  boost::swap(a1, a2);
  cout << "The array before swapping :" << a1[0] <<" "<< a2[0] << '\n';
  
  boost::array<int, 4> a3{{1,2,3,4}};   //Array with many elements
  boost::array<int, 4> a4{{10,11,12,13}};
  
  //Both Array before swapping 
  cout<<endl<<"The array before swapping : "<<endl;
  for(int i=0;i<4;i++){
   cout<<"a3["<< i <<"] :" << a3[i] <<" a4[" << i <<"] :"<< a4[i] << '\n';
  }
  
  boost::swap(a3, a4);
  cout<<endl<<"The array after swapping : "<<endl;
  for(int i=0;i<4;i++){
   cout<<"a3["<< i <<"] :" << a3[i] <<" a4[" << i <<"] :"<< a4[i] << '\n';
  }
}

Output:

The characters before swapping :c1 : x, c2 : y
The characters after swapping :c1 : y, c2 : x
The array before swapping :1 2
The array before swapping :2 1

The array before swapping : 
a3[0] :1 a4[0] :10
a3[1] :2 a4[1] :11
a3[2] :3 a4[2] :12
a3[3] :4 a4[3] :13

The array after swapping : 
a3[0] :10 a4[0] :1
a3[1] :11 a4[1] :2
a3[2] :12 a4[2] :3
a3[3] :13 a4[3] :4

More Articles of Ayush Singhal:

Name Views Likes
C++ Program to Implement CountMinSketch 1360 1
C++ boost::swap.hpp::swap 408 0

Comments