binops(3) Template functions for class-type binary operators


#include <utility>
#include <bobcat/typetrait>
#include <bobcat/binops>


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 defining the [email protected] function.

If a class supports copy construction and if it offers binary assignment operators (i.e., it offers members of the form [email protected]=), then the matching binary operators can all be implemented identically. The move-aware Class &[email protected](Class &&lhs, Class const &rhs) is easily implemented in terms of [email protected]= (note that the class itself doesn't have to be `move-aware' to define this function). The move-aware binary operator one requires a one line implementation, and as its implementation never changes it could safely be defined inline:

Class [email protected](Class &&lhs, Class const &rhs)
    return std::move(lhs @= rhs);
The traditional binary operator can be implemented using its standard form:
Class [email protected](Class const &lhs, Class const &rhs)
    Class tmp(lhs);
    tmp @= rhs;
    return tmp;
The implementation in bobcat/binops is slightly more complex as it allows from lhs or rhs promotions.

As the binary operators can all be implemented alike their definitions are perfectly suited for templates: A class offering a particular [email protected]= then automatically also offers the matching binary operators after including bobcat/binops. 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.


The binary operator functions templates in bobcat/binops are not implemented in a particular namespace. This allows sources to include bobcat/binops in multiple namespaces.

If bobcat/binops is to be used in multiple namespaces then the include safeguard (using the identifier INCLUDED_BOBCAT_BINOPS_) must be suppressed between inclusions of bobcat/binops in different namespaces.

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




The function templates in bobcat/binops implement all arithmetic binary operators, both move-aware and the traditional binary operators, expecting constant lvalue references. They can be used if the matching binary assignment operators were implemented in the classes for which the templates must be instantiated. The following operators are available:

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

`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):
    The latter group of operators also support promotions.


#include <iostream>
#include <utility>
#include "../../typetrait/typetrait"
#include "../binops"
class Demo
    friend std::ostream &operator<<(std::ostream &out, Demo const &demo);
    int d_value;
        Demo(int value = 0)
        Demo(Demo const &other)
            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';


bobcat/binops - defines the binary operator function templates


  • The header files utility, defining std::move, and bobcat/typetrait are required by, but are not included by bobcat/binops. This was a design decision, see the NAMESPACE section.


  • bobcat_4.02.00-x.dsc: detached signature;
  • bobcat_4.02.00-x.tar.gz: source archive;
  • bobcat_4.02.00-x_i386.changes: change log;
  • libbobcat1_4.02.00-x_*.deb: debian package holding the libraries;
  • libbobcat1-dev_4.02.00-x_*.deb: debian package holding the libraries, headers and manual pages;
  • public archive location;


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


Frank B. Brokken ([email protected]).