YOMM2: Fast, Open, and Multi-Methods for C++17


This library implements fast, open, multi-methods for C++17. It is strongly inspired by the papers by Peter Pirkelbauer, Yuriy Solodkyy, and Bjarne Stroustrup.


If you are familiar with the concept of open multi-methods, or if you prefer to learn by reading code, go directly to the synopsis. The reference is here

Open Methods in a Nutshell

Cross-cutting Concerns and the Expression Problem

You have a matrix math library. It deals with all sort of matrices: dense, diagonal, tri-diagonal, etc. Each matrix subtype has a corresponding class in a hierarchy rooted in Matrix.

Now you would like to render Matrix objects as JSON strings. The representation will vary depending on the exact type of the object; for example, if a matrix is a DiagonalMatrix, you only need to store the diagonal - the other elements are all zeroes.

This is an example of a "cross-cutting concern". How do you do it?

It turns out that OOP doesn't offer a good solution to this.

You can stick a pure virtual to_json function in the Matrix base class and override it in the subclasses. It is an easy solution but it has severe drawbacks. It requires you to change the Matrix class and its subclasses, and recompile the library. And now all the applications that use it will contain the to_json functions even if they don't need them, because of the way virtual functions are implemented.

Or you may resort on a "type switch": have the application test for each category and generate the JSON accordingly. This is tedious, error prone and, above all, not extensible. Adding a new matrix subclass requires updating all the type switches. The Visitor pattern also suffers from this flaw.

Wouldn't it be nice if you could add behavior to existing types, just as easily and unintrusively as you can extend existing class hierarchies via derivation? What if you could solve the so-called Expression Problem:

existing behaviors += new types
existing types += new behaviors

This is exactly what Open Methods are all about: solving the Expression Problem.

Let's look at an example.

// -----------------------------------------------------------------------------
// library code

struct matrix {
    virtual ~matrix() {}
    // ...

struct dense_matrix    : matrix { /* ... */ };
struct diagonal_matrix : matrix { /* ... */ };

// -----------------------------------------------------------------------------
// application code

#include <yorel/yomm2/keywords.hpp>

register_classes(matrix, dense_matrix, diagonal_matrix);

declare_method(std::string, to_json, (virtual_<const matrix&>));

define_method(std::string, to_json, (const dense_matrix& m)) {
    return "json for dense matrix...";

define_method(std::string, to_json, (const diagonal_matrix& m)) {
    return "json for diagonal matrix...";

int main() {

    const matrix& a = dense_matrix();
    const matrix& b = diagonal_matrix();

    std::cout << to_json(a) << "\n"; // json for dense matrix
    std::cout << to_json(b) << "\n"; // json for diagonal matrix

    return 0;

The declare_method line declares an open method called to_jsonthat takes one virtual argument of type const matrix& and returns a std::string. The virtual_<> decorator specifies that the argument must be taken into account to select the appropriate specialization. In essence, this is the same thing as having a virtual std::string to_json() const inside class Matrix - except that the virtual function lives outside of any classes, and you can add as many as you want without changing the classes.

NOTE: DO NOT specify argument names, i.e. virtual_<const matrix&> arg is not permitted.

The following define_method blocks define two implementations for the to_json method: one for dense matrices, and one for diagonal matrices.

yorel::yomm2::update_methods() must be called before any method is called, and after dynamically loading and unloading shared libraries.

Multiple Dispatch

Methods can have more than one virtual argument. This is handy in certain situations, for example to implement binary operations on matrices:

// -----------------------------------------------------------------------------
// matrix * matrix

    shared_ptr<const matrix>,
    (virtual_<shared_ptr<const matrix>>, virtual_<shared_ptr<const matrix>>));

// catch-all matrix * matrix -> dense_matrix
    shared_ptr<const matrix>,
    (shared_ptr<const matrix> a, shared_ptr<const matrix> b)) {
    return make_shared<dense_matrix>();

// diagonal_matrix * diagonal_matrix -> diagonal_matrix
    shared_ptr<const matrix>,
    (shared_ptr<const diagonal_matrix> a, shared_ptr<const diagonal_matrix> b)) {
    return make_shared<diagonal_matrix>();


Open methods are almost as fast as ordinary virtual member functions once you turn on optimization (-O2). With both clang and gcc, dispatching a call to a method with one virtual argument takes 15-30% more time than calling the equivalent virtual member function (unless the call goes through a virtual base, which requires a dynamic cast). It does not involve branching or looping, only a few memory reads (which the CPU can be parallelize), a multiplication, a bit shift, a final memory read, then an indirect call. If the body of the method does any amount of work, the difference is unnoticeable. See the implementation notes for benchmarks and assembly listings.

Building and Installing

Make sure that you have the following dependencies:

a C++17 capable compiler

cmake version 3.20 or above

Clone the repository:

git clone https://github.com/jll63/yomm2.git
cd yomm2

Create a build directory and run cmake then make:

mkdir build
cd build
cmake ..

If you want to run the tests:

make && ctest

YOMM2 uses several Boost libraries:

Preprocessor, DynamicBitset, TypeTraits: included by YOMM2 headers

Boost.Test: only used to run the test suite

If these libraries are already available on your machine, and they can be found by cmake, they will be used. In this case, make sure that the pre-installed libraries are at version 1.65 or above. If Boost is not found, the latest version will be downloaded, and the Boost headers mentioned in section (1) will be installed along YOMM2 (if you decide to make install).

If you also want to run the benchmarks (and in this case you really want a release build):

make && tests/benchmarks # wow it's fast!

This will automatically download the dependency benchmark, build it and finally install it to ./extern within the root directory of yomm2.

Finally, if you like it and you want to install it:

# either:
sudo make install
# or:
make install DESTDIR=/path/to/my/libs

This will install the library and headers, as well as a CMake package configuration.

Make sure to add the install location to CMAKE_PREFIX_PATH so that you can use find_package(YOMM2) from your including project. For linking, the use target_link_library(<your_target> YOMM2::yomm2). This will automatically add the necessary include directories, so this should be all you need to do to link to yomm2.

Going Further

The Reference is here. Since version 1.3.0, some of the internals are documented, which make it possible to use the library without using the macros - see the API tutorial.

YOMM2 has experimental support for writing templatized methods and definitions - see the templates tutorial.

The library comes with a series of examples:

The complete matrix example

The Asteroids example used in Wikipedia's article on Multiple Dispatch

Process an AST sans clumsy Visitor

Adventure: a 3-method example

friendship: an example with namespaces, method containers and friend declarations

I presented the library at CppCon 2018. Here are the video recording and the slides.

Download details:

Author: jll63
Source: https://github.com/jll63/yomm2

License: BSL-1.0 license


YOMM2: Fast, Open, and Multi-Methods for C++17
1.55 GEEK