C++ boost::operators:: left_shiftable














































C++ boost::operators:: left_shiftable



Description:-

boost::operator:: left_shiftable function used for left shift the operand.

Header file:-

#include <boost/operators.hpp>

Syntax:-

T operator<<(const T&, const T&)

where,

T is the return type of the function, T maybe the class name/structure name /template name,

operator is the keyword,

<< is used for left shift,

const T&, const T&:-they are the two objects of templates/class/structure. We need two parameters to left shift , one is value to be left shift and another one is for left shift the value by how much ,that's why we are passing two  parameters in form of objects.

Sample code:-

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <boost/operators.hpp>
#include<boost/iterator.hpp>
#include <string>
#include <utility>
#include <iostream>
struct bitwise : public boost::left_shiftable<bitwise>
{
  int n; //number
  bitwise(int l) : n(l) {}
//performing left shift
  bitwise operator<<(const bitwise &a) const{ return n << a.n; }
};

int main()
{
//passing parameters
  bitwise a1(3);
  bitwise a2(8);
//printing output
  std::cout << std::dec << (a1.n << a2.n) << '\n';
}

Output:-

768


Comments