binops(3bobcat)
π NAME π
okay so bestie, meet binops β it's from the binops(3bobcat) page, aka the Binary Operators girlie π. template functions for class-type binary operators. that's the whole vibe. no cap.
β¨ SYNOPSIS β¨
#include <utility>
#include <bobcat/typetrait>
#include <bobcat/binops>
three includes, that's the whole entrance. slay π
π¦ DESCRIPTION π¦
so here's the tea, bestie. classes can overload binary operators. like, a class named Class can overload these binary operators to suit its own needs β which means, for example, two Class type objects can literally be added once you've overloaded operator+. π
the binary operators you can overload are *, /, %, +, -, <<, >>, &, |, and ^ β and in this man page they're all generically referred to as the @ operator. you overload one by defining the operator@ function. periodt.
now here's where it gets iconic: if a class supports copy construction and it offers binary assignment operators (i.e., it offers members of the form operator@=), then all the matching binary operators can be implemented identically. like, the exact same way every single time. π
the move-aware Class &operator@(Class &&lhs, Class const &rhs) is easily implemented in terms of operator@= (and note that the class itself doesn't have to be "move-aware" to define this function, iykyk). the move-aware binary operator just needs a one line implementation, and since its implementation never changes it could safely be defined inline:
Class operator@(Class &&lhs, Class const &rhs)
{
return std::move(std::move(lhs) @= rhs);
}
and the traditional binary operator can be implemented using its standard form:
Class operator@(Class const &lhs, Class const &rhs)
{
Class tmp(lhs);
tmp @= rhs;
return tmp;
}
the implementation in bobcat/binops is slightly more complex, because it allows from lhs or rhs promotions. that's the little extra spice πΆοΈ
and because the binary operators can all be implemented alike, their definitions are perfectly suited for templates β chef's kiss π. so a class offering a particular operator@= automatically also offers the matching binary operators after including bobcat/binops. and since the binary function templates are not instantiated until they're used, their definitions can be processed by the compiler even if a class implements only a subset of the available binary assignment operators. no crash, no drama, it just works. π
πΈ NAMESPACE πΈ
plot twist: the binary operator function templates in bobcat/binops are not implemented in a particular namespace. that's on purpose, bestie β it allows sources to include bobcat/binops in multiple namespaces.
heads up though: ifbobcat/binopsis going to be used in multiple namespaces, then the include safeguard (using the identifierINCLUDED_BOBCAT_BINOPS_) must be suppressed between inclusions ofbobcat/binopsin different namespaces. don't skip this step, fr fr. π
e.g., to make the binary operator function templates available in a source file using the namespace FBB and in a source file using the default namespace, the following scheme can be used:
#include <utility> // ensure std::move is available
#include <bobcat/typetrait> // required by binops
namespace MY_NAMESPACE
{
#include <bobcat/binops> // binary operators available in MY_NAMESPACE
}
#undef INCLUDED_BOBCAT_BINOPS_ // suppress the include guard
#include <bobcat/binops> // read binops again so the binary
// operators can be used in the
// default namespace as well
see how it works? undef, re-include, boom β both namespaces get the goods. π
π§ INHERITS FROM π§
-
(yep, nothing. it inherits from nobody. independent queen energy. π)
π₯ OVERLOADED OPERATORS π₯
the function templates in bobcat/binops implement all arithmetic binary operators β both move-aware and the traditional binary operators, expecting constant lvalue references. you can use them if the matching binary assignment operators were implemented in the classes for which the templates must be instantiated. here's the full roster: β
move-aware operators, using temporary objects for its left-hand side operands:
Class operator*(Class &&lhs, Class const &rhs)Class operator/(Class &&lhs, Class const &rhs)Class operator%(Class &&lhs, Class const &rhs)Class operator+(Class &&lhs, Class const &rhs)Class operator-(Class &&lhs, Class const &rhs)Class operator<<(Class &&lhs, Class const &rhs)Class operator>>(Class &&lhs, Class const &rhs)Class operator&(Class &&lhs, Class const &rhs)Class operator|(Class &&lhs, Class const &rhs)Class operator^(Class &&lhs, Class const &rhs)
and the "traditional" operators, using lvalue references to constant objects for its left-hand side operands:
Class operator*(Class const &lhs, Class const &rhs)Class operator/(Class const &lhs, Class const &rhs)Class operator%(Class const &lhs, Class const &rhs)Class operator+(Class const &lhs, Class const &rhs)Class operator-(Class const &lhs, Class const &rhs)Class operator<<(Class const &lhs, Class const &rhs)Class operator>>(Class const &lhs, Class const &rhs)Class operator&(Class const &lhs, Class const &rhs)Class operator|(Class const &lhs, Class const &rhs)Class operator^(Class const &lhs, Class const &rhs)- this latter group of operators also supports promotions. π«Ά
π EXAMPLE π
okay bestie, actual working code, look at her go:
#include <iostream>
#include <utility>
#include <bobcat//typetrait>
#include <bobcat/binops>
class Demo
{
friend std::ostream &operator<<(std::ostream &out, Demo const &demo);
int d_value;
public:
Demo(int value = 0)
:
d_value(value)
{}
Demo(Demo const &other)
:
d_value(other.d_value)
{
std::cout << "Demo CC called\n";
}
Demo &operator+=(Demo const &rhs)
{
d_value += rhs.d_value;
return *this;
}
};
std::ostream &operator<<(std::ostream &out, Demo const &demo)
{
return out << demo.d_value;
}
using namespace std;
int main()
{
Demo four(4);
Demo five(5);
cout << four + five << β\nβ <<
four + 5 << β\nβ <<
4 + five << β\nβ;
}
see? only operator+= was hand-written and yet four + five, four + 5 and 4 + five all just work. promotions said yes. the template ate. π₯
π FILES π
bobcat/binopsβ defines the binary operator function templates
π SEE ALSO π
bobcat/binopsbase(3)bobcat(7)
π BUGS π
- the header files
utility, definingstd::move, andbobcat/typetraitare required by, but are not included bybobcat/binops. (not a mistake, bestie β this was a design decision, see the NAMESPACE section above. π)
β 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". π± iconic name, honestly.
πΈ 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). thank you legend π«‘
and that's the whole page, bestie!! this was binops(3bobcat), shipped out of libbobcat-dev_6.15.01, dated 2005-2026 β yes she's been serving since 2005 and she's still going. π periodt. π
β¨
π¦ 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