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 π
https://fbb-git.gitlab.io/bobcat/β gitlab project page
Debian Bobcat project files:
libbobcat6β debian package containing the shared library, changelog and copyright notelibbobcat-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β.
π 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).
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
- ubuntu stonking: libbobcat-dev 6.15.01-1 Β· GPL-3.0-or-later WITH OpenSSL exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms