binopsbase(3bobcat)

Binary Operators
(libbobcat-dev_6.04.00)

2005-2023

NAME

BinopsBase - Class template offering class-type binary operators

SYNOPSIS

#include <bobcat/binopsbase>

DESCRIPTION

Classes may overload binary operators. A class named Derived may overload binary operators to suit its own needs. It could, e.g., allow two Derived class objects to be added together, or it could define a shift-operation given a size_t right-hand side (rhs) argument.

The available binary operators are *, /, %, +, -, <<, >>, &, |, and ^ (in this man-page they are generically indicated as the `@' operator). In addition, overloaded operator<< and operator>> for stream insertion and extraction are frequently defined.

If a class Derived supports copy and/or move construction and if it offers a swap member (void Derived::swap(Derived &rhs)), and is publicly derived from FBB::BinopsBase<Derived> then once Derived defines a member


    void Class::operator@=(Rhs const &rhs) &&
        
defining the compound @-operator for anonymous, temporary Derived objects and a Rhs type for its right-hand side operand the following operators are also available:

    Derived &operator@=(Rhs const &rhs) &

    Derived operator@(Derived &&lhs, Rhs const &rhs);
    Derived operator@(Derived const &lhs, Rhs const &rhs);
        

A similar procedure applies to the insertion and extraction operators. Insertion and extraction operators become available once BinopsBase<Derived> is declared a friend class of Derived. To make the insertion operator available a private member


    void Derived::insert(std::ostream &out) const
        
must then also be defined, inserting the calling object into out. Analogously, to make the extraction operator available a private member

    void Derived::extract(std::istream &in)
        
must be defined extrating the calling object from in.

INHERITS FROM

--

OVERLOADED OPERATORS

For each defined Derived &&operator@=(Rhs const &rhs) &&, defined in the class Derived the following operators are automatically also available:


    Derived operator@(Derived &&lhs, Rhs const &rhs);
    Derived operator@(Derived const &lhs, Rhs const &rhs);
        
and

    Derived &operator@=(Rhs const &rhs) &;
        

The insertion operator becomew available once


    void Derived::insert(std::ostream &out) const
        
(inserting the calling object into out) has been defined. Analogously, the extraction operator becomes available after defining a private member

    void Derived::extract(std::istream &in)
        
(extracting the calling object from in).

FRIEND DECLARATION

To make the insertion and/or extraction operators available the class Derived must also declare


    friend FBB::BinopsBase<Derived>;
        

EXAMPLE

#include <iostream>
#include <bobcat/binopsbase>

class Demo1: public FBB::BinopsBase<Demo1>
{
    friend FBB::BinopsBase<Demo1>;            // for insert/extract

    public:
        void swap(Demo1 &other)
        {}

        Demo1 &&operator+=(Demo1 const &rhs) &&
        {
            std::cout << "adding two Demo1 objects\n";
            return std::move(*this);
        }

//  Explicit definitions take precedence over functions templates
//  instantiated from definitions in bobcat/binopsbase.
//        Demo1 &operator+=(Demo1 const &rhs) &
//        {
//            std::cout << "adding (self-defined) two Demo1 objects\n";
//            return *this;
//        }

        Demo1 &&operator-=(Demo1 const &rhs) &&
        {
            std::cout << "subtracting two Demo1 objects\n";
            return std::move(*this);
        }

        Demo1 &&operator<<=(Demo1 const &rhs) &&
        {
            std::cout << "shiftleft on two Demo1 objects\n";
            return std::move(*this);
        }

        Demo1 &&operator<<=(size_t rhs) &&
        {
            std::cout << "shiftleft Demo1 object size_t bits\n";
            return std::move(*this);
        }

        void insert(std::ostream &out) const        // requires friend
        {
            out << "inerting a Demo1 object\n";
        }

        void extract(std::istream &in)              // requires friend
        {
            std::cout << "extracting a Demo1 object\n";
        }
};

class Demo2: public FBB::BinopsBase<Demo2>
{
    public:
        void swap(Demo2 &other)
        {}

        Demo2 &&operator+=(Demo2 const &rhs) &&
        {
            std::cout << "adding two Demo2 objects\n";
            return std::move(*this);
        }

        Demo2 &operator+=(Demo2 const &rhs) &
        {
            std::cout << "adding (self-defined) two Demo2 objects";
            return *this;
        }

        Demo2 &&operator^=(Demo2 const &rhs) &&
        {
            std::cout << "xor-ing two Demo2 objects\n";
            return std::move(*this);
        }

        Demo2 &&operator|=(Demo2 const &rhs) &&
        {
            std::cout << "or-ing two Demo2 objects\n";
            return std::move(*this);
        }

};

int main()
{
    Demo1 d1a, d1b;
    Demo1 d1c = d1a + d1b;
    d1a += d1b;
    d1c = Demo1{} + d1b;

    std::cout << "Here we are " << d1c << '\n';
    std::cin >> d1c;

    d1a <<= d1a;
    d1a <<= 5;

    Demo2 d2a, d2b;
    Demo2 d2c = d2a + d2b;
    d2a ^= d2b;
    d2c = Demo2{} ^ d2b;

    d2c = d2c | d2b;
}

FILES

bobcat/binopsbase - defines the binary operator function templates

SEE ALSO

bobcat/binops(3), bobcat(7)

BUGS

None Reported.

BOBCAT PROJECT FILES

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).