The Art of C++ / Operators
The Art of C++ / Operators is a zero-dependency C++11 single-header library that provides highly efficient, move aware operators for arithmetic data types.
Table of Content
Overview
Example
Requirements
Installation
Provided Templates
Commutativity
RValue References
constexpr
noexcept
nodiscard
Changelog
History
License
Overview
Overloaded operators for class types typically don't come alone.
For example, when x + y is possible then x += y should be, too.
When x < y is possible then x > y, x >= y, and x <= y should be, too.
Implementing large sets of operators, possibly for multiple classes, is both tedious and error-prone.
However, more often than not, some of these operators can be defined in terms of others.
For example x >= y can frequently be defined as !(x < y).
Given the implementation of some basic operators, the templates in the Art of C++ / Operators can generate many more operators automatically.
The generated operators are overloaded to take advantage of movable types, and allow the compiler to avoid unneccessary temporary objects wherever possible.
All generated operators are noexcept when the underlying operations are noexcept.
Generated comparison operators are constexpr (when supported by the compiler).
Example
Given this dummy integer class...
#include <tao/operators.hpp>
class MyInt
: tao::operators::commutative_addable< MyInt >,
tao::operators::multipliable< MyInt, double >
{
public:
// create a new instance of MyInt
MyInt( const int v ) noexcept;
// copy and move constructor
MyInt( const MyInt& v ) noexcept;
MyInt( MyInt&& v ) noexcept; // optional
// copy and move assignment
MyInt& operator=( const MyInt& v ) noexcept;
MyInt& operator=( MyInt&& v ) noexcept; // optional
// addition of another MyInt
MyInt& operator+=( const MyInt& v ) noexcept;
MyInt& operator+=( MyInt&& v ) noexcept; // optional
// multiplication by a scalar
MyInt& operator*=( const double v ) noexcept;
};
...the base class templates will generate the following operators.
// generated by tao::operators::commutative_addable< MyInt >
MyInt operator+( const MyInt& lhs, const MyInt& rhs ) noexcept;
MyInt&& operator+( const MyInt& lhs, MyInt&& rhs ) noexcept;
MyInt&& operator+( MyInt&& lhs, const MyInt& rhs ) noexcept;
MyInt&& operator+( MyInt&& lhs, MyInt&& rhs ) noexcept;
// generated by tao::operators::multipliable< MyInt, double >
MyInt operator*( const MyInt& lhs, const double& rhs ) noexcept;
MyInt operator*( const MyInt& lhs, double&& rhs ) noexcept;
MyInt&& operator*( MyInt&& lhs, const double& rhs ) noexcept;
MyInt&& operator*( MyInt&& lhs, double&& rhs ) noexcept;
Note: The // optional in class MyInt above marks methods
that you typically only add when your class benefits from an
rvalue reference parameter. If there is no benefit for the
implementation, you can just omit these methods. If you leave
them out, The Art of C++ / Operators will simply call the corresponding
non-movable version that takes the parameter by const lvalue
reference.
Requirements
Requires C++11 or newer. Tested with:
- GCC 4.7+
- Clang 3.2+
- Visual Studio 2015+
Remember to enable C++11, e.g., provide -std=c++11 or similar options.
Note: If you use or test the library with other compilers/versions,
e.g., Visual C++, Intel C++, or any other compiler we'd like to hear from you.
Note: For compilers that don't support noexcept, see chapter noexcept.
Installation
The Art of C++ / Operators is a single-header library. There is nothing to build or install,
just copy the header somewhere and include it in your code.
Package Managers
You can download and install taocpp-operators using the Conan package manager:
conan install taocpp-operators/1.2.2@
The taocpp-operators package in conan is kept up to date by Conan team members and community contributors.
If the version is out-of-date, please create an issue or pull request on the Conan Center Index repository.
Provided Templates
The following table gives an overview of the available templates.
Note that the "Provides" and "Requires" columns are just a basic overview.
Multiple overloads per provided operator might exist to ensure the most
efficient implementation for each case, exploiting move-semantics when
possible and (unless explicitly disabled) pass-through of temporary
values to avoid creating new temporaries.
Each overload of an operator is marked noexcept when the required operation(s)
that are used to implement it are also marked noexcept.