Scroll to navigation

binopsbase(3bobcat) Binary Operators binopsbase(3bobcat)

NAME

binopsbase - Class template offering class-type binary operators

SYNOPSIS

#include <bobcat/binopsbase>

DESCRIPTION

Classes can overload binary operators. A class named Class may overload these binary operators to suit its own needs, allowing, e.g., two Class type objects to be added after overloading operator+. Operators for the binary operators *, /, %, +, -, <<, >>, &, |, and ^ (in this man-page they are generically indicated as the `@’ operator) can be overloaded by providing various operator@ members and functions.

If a class supports copy and/or move construction and if it offers a swap member, then binary operators can all be implemented identically, except for the specific details associated with specific binary operators. E.g., if a class contains two int data members addition of objects of that class could simply be defined as adding the corresponding int members, while subtraction could be defined as subtracting the corresponding int members. Assuming the existence of a member void Class::add(Class const &rhs) that defines the addition of a Class rhs object to the *this object, while merely providing the basic exception guarantee (i.e., no leakage), then the binary addition operators can be defined like this:

Class operator+(Class &&lhs, Class const &rhs)
{
    lhs.add(rhs);
    return std::move(lhs);
}
Class operator+(Class const &lhs, Class const &rhs)
{
    Class tmp(lhs);
    tmp.add(rhs);
    return tmp;
}
        
Likewise, operator+= can be defined for lvalue or rvalue Class objects using reference modifiers.

As binary operators can all be implemented alike, given the specific members implementing the implied operations, these operators can very well be provided using templates.

By inheriting from the class template BinopsBase classes offering such specific functions can a particular operator@= then automatically also offers the matching binary operators after including bobcat/binopsbase. Since the binary function templates are not instantiated until used their definitions can be processed by the compiler even if a class implements only a subset of the available binary assignment operators.

To provide a class Class with binary operators (and binary compound operators) the class Class must implement a member void swap(Class &other), and it should implement (a subset of) the following members (preferably in the class’s private section):

o
void mul(Class const &rhs) - to provide operators * and *=
o
void div(Class const &rhs) - to provide operators / and /=
o
void mod(Class const &rhs) - to provide operators % and %=
o
void add(Class const &rhs) - to provide operators + and +=
o
void sub(Class const &rhs) - to provide operators - and -=
o
void shl(Class const &rhs) - to provide operators << and <<=
o
void shr(Class const &rhs) - to provide operators >> and >>=
o
void and_(Class const &rhs) - to provide operators & and &=
o
void or_(Class const &rhs) - to provide operators | and |=
o
void xor_(Class const &rhs) - to provide operators ^ and ^= Note the trailing underscore for the members and_, or_, and xor_: the underscores are used to prevent conflicts with the and, or, and xor keywords.

Next, Class should publicly inherit from

    FBB::BinopsBase<Class, int ...operators>
        
where int ...operators lists the specific set of binary operators that are requested. E.g., to provide Class with multiplication, division, modulo, and shit-operators its class interface should start as shown:
    class Class: public FBB::BinopsBase<Class, ’*’, ’/’, ’%’, ’<’, ’>’>
        
There is no specific ordering required when specifying int ...operators. Note that ’<’ and ’>’ in the int ...operators list specify, respectively, the shift-left and shift-right operators. All other operators are specified by their common operator, specified as character constants.

If, as advised, the members implementing the required binary operations are declared in the class’s private section then the class’s interface should declare its base class as a friend. E.g.,

    class Class: public FBB::BinopsBase<Class, ’*’, ’/’, ’%’, ’<’, ’>’>
    {
        friend FBB::BinopsBase<Class, ’*’, ’/’, ’%’, ’<’, ’>’>;
        public:
            ...
        private:
            void mul(Class const &rhs);
            void div(Class const &rhs);
            void mod(Class const &rhs);
            void shl(Class const &rhs);
            void shr(Class const &rhs);
    };
        

INHERITS FROM

Several Internal-use-only Bobcat classes, completing the BinopsBase class template.

OVERLOADED OPERATORS

Assuming BinopsBase is a base class of the class Class, then for each of the requested binary operators (e.g., @) the following operators are now available:

    free operators:
        Class operator@(Class const &lhs, Class const &rhs);
        Class operator@(Class &&lhs, Class const &rhs);
    members:
        Class &operator@(Class const &rhs) &;
        Class &&operator@(Class const &rhs) &&;
    

EXAMPLE

#include "../binopsbase"
#include <iostream>
using namespace std;
class Demo1: public FBB::BinopsBase<Demo1, ’+’, ’-’>
{
    friend FBB::BinopsBase<Demo1, ’+’, ’-’>;
    public:
        void swap(Demo1 &other)
        {}
    private:
        void add(Demo1 const &rhs)
        {
            cout << "adding two Demo1 objects\n";
        }
        void sub(Demo1 const &rhs)
        {
            cout << "subtracting two Demo1 objects\n";
        }
};
class Demo2: public FBB::BinopsBase<Demo2, ’+’, ’^’>
{
    friend FBB::BinopsBase<Demo2, ’+’, ’^’>;
    public:
        void swap(Demo2 &other)
        {}
    private:
        void add(Demo2 const &rhs)
        {
            cout << "adding two Demo2 objects\n";
        }
        void xor_(Demo2 const &rhs)
        {
            cout << "xor-ing two Demo2 objects\n";
        }
};
int main()
{
    Demo1 d1a, d1b;
    Demo1 d1c = d1a + d1b;
    d1a += d1b;
    d1c = Demo1{} + d1b;
    Demo2 d2a, d2b;
    Demo2 d2c = d2a + d2b;
    d2a ^= d2b;
    d2c = Demo2{} ^ d2b;
}

FILES

bobcat/binopsbase - defines the binary operator function templates

SEE ALSO

bobcat/binops(3) bobcat(7)

BUGS

None Reported.

DISTRIBUTION FILES

o
bobcat_4.04.00-x.dsc: detached signature;
o
bobcat_4.04.00-x.tar.gz: source archive;
o
bobcat_4.04.00-x_i386.changes: change log;
o
libbobcat1_4.04.00-x_*.deb: debian package holding the libraries;
o
libbobcat1-dev_4.04.00-x_*.deb: debian package holding the libraries, headers and manual pages;
o
http://sourceforge.net/projects/bobcat: public archive location;

BOBCAT

Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.

COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

AUTHOR

Frank B. Brokken (f.b.brokken@rug.nl).
2005-2016 libbobcat-dev_4.04.00-x.tar.gz