C++ boost::operators::addable














































C++ boost::operators::addable



Description:-

boost::operator:: addable function used to add the operands.

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 addition,

const T&, const T&:-they are the two objects of templates/class/structure. We need two parameters to add 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 num : public boost::addable<num>
{
int n; //number
num(int l) : n(l) {}
//adding the numbers
num operator+(const num &a) const{ return n + a.n; }
};

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

Output:-

11


Comments