binopsbase(3bobcat)

πŸ’–βœ¨ binopsbase β€” from the bobcat manual, section 3 (3bobcat) πŸŽ€πŸ¦‹

okay bestie, sit down, hydrate, and let me tell you about the single most slept-on header in the bobcat fam. this is the one that writes your binary operators for you so you can stop copy-pasting them like it's 2009. it's giving "let the template do the cardio." πŸ’…


🌸 NAME 🌸

BinopsBase β€” a class template that hands you class-type binary operators, no cap 🫢


⭐ SYNOPSIS ⭐

#include <bobcat/binopsbase>

πŸ‘‘ DESCRIPTION πŸ‘‘

so, classes can overload binary operators, right? a class called Derived can overload binary operators however it wants. it could, like, let two Derived objects be added together, or it could define a shift-operation that takes a size_t right-hand side (rhs) argument. main character energy fr fr.

the binary operators on the menu are *, /, %, +, -, <<, >>, &, |, and ^ β€” and in this man-page we just generically call them the @ operator. plus, overloaded operator<< and operator>> for stream insertion and extraction get defined constantly, you already know.

here's the tea β˜• β€” if a class Derived:

  • supports copy and/or move construction, and
  • offers a swap member, like void Derived::swap(Derived &rhs), and
  • is publicly derived from FBB::BinopsBase<Derived>

…then once Derived defines this one member:

void Class::operator@=(Rhs const &rhs) &&

β€” which is, like, the compound @-operator but for anonymous, temporary Derived objects, with a Rhs type for its right-hand side operand β€”

the following operators are also available, automatically, no extra work:

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. ✨ and to actually make the insertion operator available, a private member

void Derived::insert(std::ostream &out) const

must then also be defined, which inserts the calling object into out. analogously, to make the extraction operator available, a private member

void Derived::extract(std::istream &in)

must be defined, extracting the calling object from in. yes, private. yes, that's why the friendship is there. the friend is doing the heavy lifting bestie 😭🫢


πŸ¦‹ INHERITS FROM πŸ¦‹

--

(yeah. nothing. it's a template, it just is. iconic.) πŸŽ€


πŸ”₯ OVERLOADED OPERATORS πŸ”₯

for every defined Derived &&operator@=(Rhs const &rhs) && that's 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 also:

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

the insertion operator becomes available once

void Derived::insert(std::ostream &out) const

β€” which inserts 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)

β€” which extracts the calling object from in. periodt. πŸ’…


🌈 FRIEND DECLARATION 🌈

to make the insertion and/or extraction operators available, the class Derived must also declare:

friend FBB::BinopsBase<Derived>;

one line. that's it. that's the whole vibe check. πŸ”‘


πŸ“ 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;
}

lowkey the Demo2 one is the glow-up: it defines its own Demo2 &operator+=(Demo2 const &rhs) & and that explicit definition takes precedence over the function templates instantiated from the definitions in bobcat/binopsbase. look at the comment block in Demo1 β€” it's showing you the same trick, just commented out. ate. πŸ’œ


🧚 FILES 🧚

  • bobcat/binopsbase β€” defines the binary operator function templates

🌟 SEE ALSO 🌟


πŸ’€ BUGS πŸ’€

None Reported. (we love a clean bug report bestie πŸŽ€)


πŸŽ€ BOBCAT PROJECT FILES πŸŽ€

Debian Bobcat project files:

  • libbobcat6 β€” debian package containing the shared library, changelog and copyright note
  • libbobcat-dev β€” debian package containing the static library, headers, manual pages, and developer info

πŸ‘‘ BOBCAT πŸ‘‘

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


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


🌸 AUTHOR 🌸

Frank B. Brokken (f.b.brokken@rug.nl).


no cap, that's the whole page. now go add friend FBB::BinopsBase<Derived>; and free yourself 🫢✨

that's a wrap on the bobcat manual, section 3 β€” shipped as libbobcat-dev_6.15.01, dated 2005-2026. mwah. πŸ’‹

πŸ“¦ where this page comes from