C++ boost::operators::equality_comparable














































C++ boost::operators::equality_comparable



Description:-

boost::operator:: equality_comparable function check the operands are not equal,if not then returns false.Its return type is bool.

Header file-

#include <boost/operators.hpp>

Syntax:-

bool operator==(const T&, const T&)

where,

bool is the return type of the function,

operator is the keyword,

== is used for comparison of equality_comparable,

const T&, const T&:-they are the two objects of templates/class/structure. we need two parameter so to compare that's                                           why we are passing two  parameter in form of objects.

Sample Code:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <boost/operators.hpp>
#include <string>
#include <utility>
#include <iostream>
struct shape : public boost::equality_comparable<shape>
{
std::string name; //for shape name
int edges; //number edges that shape has
shape(std::string n, int l) : name(std::string(n)), edges(l) {}
//checks for equality whether number of edges are equal or not
bool operator==(const shape &a) const { return edges == a.edges; }
};
int main()
{
shape a1("triangle", 3);
shape a2("octagon", 8);
std::cout << std::boolalpha << (a2 == a1) << '\n';
}

Output:-

false



Comments