Troubleshooting

Contents

OpenMethod catches most mistakes at compile time. The rest surface at run time: initialize reports inconsistencies in the registered classes, methods and overriders, and a method call reports a combination of arguments that it cannot dispatch. This page lists every error that the library reports, each under its message, with a short example that produces it.

Compilation errors

OpenMethod is a library that emulates a language feature. A mistake therefore surfaces as a failed template instantiation, and the compiler reports it together with the whole instantiation context, much of it inside the library. Where the library can tell what went wrong, it says so in a static_assert. Search the output for the message:

Compiler Diagnostic

gcc

static assertion failed: message

clang

static assertion failed due to requirement '…​': message

MSVC

error C2338: static assertion failed: 'message'

Read the first one. The errors that follow it are usually its consequences. In particular, after an error in an overrider, gcc also reports BOOST_OPENMETHOD_OVERRIDE: the overrider does not match the method or virtual_ptr category mismatch, and after an error in a method, a static assertion failed with no message. These add nothing to the first error.

The examples below start from these declarations:

#include <iostream>

#include <boost/openmethod.hpp>

using namespace boost::openmethod;

struct Node {
    virtual ~Node() = default;
};

struct Plus : Node {};

struct my_registry : default_registry::with<policies::runtime_checks> {};

Methods

method has no virtual parameters

Example - Compiler Explorer

BOOST_OPENMETHOD(postfix, (Node&, std::ostream&), void);

A method dispatches on the parameters declared with virtual_ or virtual_ptr. Without one, it is an ordinary function.

Declare the parameters to dispatch on as virtual. Here, the first parameter becomes virtual_<Node&>. The method then selects an overrider from the dynamic type of the node.

Fix

BOOST_OPENMETHOD(postfix, (virtual_<Node&>, std::ostream&), void);

Or as a virtual_ptr<Node>.

Alternative fix

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

virtual_traits not specialized for type

Example - Compiler Explorer

BOOST_OPENMETHOD(postfix, (virtual_<Node>, std::ostream&), void);

A virtual_ parameter is a reference, a pointer, or a smart pointer that OpenMethod supports. Dispatching on an object passed by value would slice it.

Pass the object by reference: virtual_<Node&>, not virtual_<Node>. The method then sees the object itself, with its dynamic type.

Fix

BOOST_OPENMETHOD(postfix, (virtual_<Node&>, std::ostream&), void);

virtual_<> parameter is not a polymorphic class…​

Example - Compiler Explorer

struct Literal {};

BOOST_OPENMETHOD(postfix, (virtual_<Literal&>, std::ostream&), void);

The default registry finds the dynamic type of an object with typeid, which yields it only for a polymorphic class. See also no matching function for call to 'method', the same mistake made with virtual_ptr.

Give the root of the hierarchy a virtual destructor. It makes the class polymorphic, and typeid then yields the dynamic type.

Fix

struct Literal {
    virtual ~Literal() = default;
};

BOOST_OPENMETHOD(postfix, (virtual_<Literal&>, std::ostream&), void);

virtual_ptr<> must be passed by value or by const reference

Example - Compiler Explorer

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>&, std::ostream&), void);

A method takes a virtual_ptr by value - it is a pair of pointers, cheap to copy - or by const reference. It cannot take it by non-const reference, or by rvalue reference, because it may have to build one from its argument.

Take the virtual_ptr by value, or by const reference. The method can then build it from any argument that converts to it, including a virtual_ptr to a derived class, or a reference to the object.

Fix

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

Or by const reference.

Alternative fix

BOOST_OPENMETHOD(postfix, (const virtual_ptr<Node>&, std::ostream&), void);

virtual_ptr<> takes a class or a smart pointer: virtual_ptr<Node>, not virtual_ptr<Node*>

Example - Compiler Explorer

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node*>, std::ostream&), void);

void print(Node* node) {
    postfix(node, std::cout);
}

A virtual_ptr is a pointer already. The method declaration compiles, but the first use of virtual_ptr<Node*> does not.

Name the class: virtual_ptr<Node>. A virtual_ptr<Node> is built from a Node*, so the call does not change. To dispatch through a smart pointer, name the smart pointer instead: virtual_ptr<std::shared_ptr<Node>>.

Fix

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

void print(Node* node) {
    postfix(node, std::cout);
}

Overriders

BOOST_OPENMETHOD_OVERRIDE: cannot find '…​' method that accepts the same arguments as the overrider

Example - Compiler Explorer

class Minus : Node {};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Minus>, std::ostream& os), void) {
    os << "-";
}

BOOST_OPENMETHOD_OVERRIDE does not take the method as an argument: it finds the method by overload resolution, looking for a method of the same name that can be called with the overrider’s arguments. Here Minus derives from Node privately - class makes the inheritance private unless told otherwise - so a virtual_ptr<Minus> does not convert to a virtual_ptr<Node>, and postfix cannot be called with one.

The other causes of the same message are:

  • The method is not declared, or not before the overrider.

  • The name is misspelled.

  • The class of a virtual parameter does not derive from the class in the same position in the method, or does so ambiguously.

  • The overrider has more, or fewer, parameters than the method.

  • A virtual parameter is a reference in the method and a pointer in the overrider, or the other way around.

  • The method takes a virtual_ and the overrider a virtual_ptr. The overrider takes what the method takes, without the virtual_: Plus& for virtual_<Node&>.

  • The method takes a smart virtual_ptr, such as shared_virtual_ptr, and the overrider a plain virtual_ptr, or one over another kind of smart pointer. The opposite mistake has a message of its own: different virtual_ptr<> pointer kinds.

  • A virtual parameter of the overrider is const where the method’s is not. The opposite mistake has a message of its own: overrider parameter discards const qualifier.

  • A non-virtual parameter differs from the method’s in a way that no conversion bridges. When a conversion does, the message is non-virtual parameter types must match exactly.

  • The method is in another namespace, and its arguments do not make it visible through argument-dependent lookup. See Namespaces.

When the overrider and the method differ only by their registry, the message says so instead: registry mismatch.

In the example, make the inheritance public: class Minus : public Node. A virtual_ptr<Minus> then converts to a virtual_ptr<Node>, and overload resolution finds postfix. For the other causes, make the overrider callable the same way: declare the method first, correct the name, or match the parameters to the method’s.

Fix

class Minus : public Node {};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Minus>, std::ostream& os), void) {
    os << "-";
}

overrider parameter discards const qualifier

Example - Compiler Explorer

BOOST_OPENMETHOD(postfix, (virtual_ptr<const Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

The method takes a virtual_ptr<const Node>: it promises not to modify the node. The overrider for Plus takes a virtual_ptr<Plus>, through which it could. When the method calls the overrider, it casts its argument to the type of the overrider’s parameter, and a cast cannot remove const. Clang follows the message with the error from the cast itself, which names the two classes: dynamic_cast from 'const Node' to 'Plus &' casts away qualifiers.

Make the overrider’s parameter const as well: virtual_ptr<const Plus>. The cast then keeps the const, and the overrider keeps the promise of the method. The same applies to references and pointers: an overrider for virtual_<const Node&> takes a const Plus&.

Fix

BOOST_OPENMETHOD(postfix, (virtual_ptr<const Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<const Plus>, std::ostream& os), void) {
    os << "+";
}

If the overrider is right, and the method is meant to let its overriders modify the node, drop the const from the method instead.

Alternative fix

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

virtual_ptr<> is required in overrider in same position as in method

Example - Compiler Explorer

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(postfix, (Plus&, std::ostream& os), void) {
    os << "+";
}

The method takes a virtual_ptr<Node>, and the overrider a Plus& in the same position. Where the method takes a virtual_ptr, the overrider takes one too.

Take a virtual_ptr to the class of the overrider: virtual_ptr<Plus>. The method passes its argument on, cast to that class.

Fix

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

If the overriders are meant to take references, declare the parameter of the method as virtual_<Node&> instead. The overrider then takes a Plus&.

Alternative fix

BOOST_OPENMETHOD(postfix, (virtual_<Node&>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(postfix, (Plus&, std::ostream& os), void) {
    os << "+";
}

different virtual_ptr<> reference categories

Example - Compiler Explorer

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (const virtual_ptr<Plus>&, std::ostream& os), void) {
    os << "+";
}

The method takes the virtual_ptr by value, and the overrider by const reference.

Use the same form in both: here, by value, as the method does. The overrider then receives the virtual_ptr the way the method passes it.

Fix

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

Or by const reference, in both.

Alternative fix

BOOST_OPENMETHOD(postfix, (const virtual_ptr<Node>&, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (const virtual_ptr<Plus>&, std::ostream& os), void) {
    os << "+";
}

different virtual_ptr<> pointer kinds

Example - Compiler Explorer

#include <boost/openmethod/interop/std_shared_ptr.hpp>

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (shared_virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

The method takes a plain virtual_ptr, and the overrider a smart one.

Use the same kind in both: virtual_ptr<Plus> for virtual_ptr<Node>, shared_virtual_ptr<Plus> for shared_virtual_ptr<Node>. A plain pointer cannot be turned into a smart one, so the overrider takes what the method holds.

Fix

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

If the overrider is meant to share ownership of the node, make the method take a shared_virtual_ptr<Node> instead.

Alternative fix

#include <boost/openmethod/interop/std_shared_ptr.hpp>

BOOST_OPENMETHOD(postfix, (shared_virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (shared_virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

non-virtual parameter types must match exactly

Example - Compiler Explorer

#include <fstream>

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ofstream& os), void) {
    os << "+";
}

The non-virtual parameters of an overrider have the same types as the method’s. Here, std::ostream& would have to be cast to std::ofstream&, which cannot be done safely.

Declare the parameter with the type of the method: std::ostream&. The overrider then receives the argument as the caller passed it.

Fix

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

If the method is meant for file streams only, declare its parameter as std::ofstream& instead.

Alternative fix

#include <fstream>

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ofstream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ofstream& os), void) {
    os << "+";
}

overrider return type is not convertible to method return type

Example - Compiler Explorer

BOOST_OPENMETHOD(value, (virtual_ptr<Node>), int);

BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Plus>), std::string) {
    return "+";
}

An overrider returns the method’s return type, or a type that converts to it, such as Plus* for Node*. If the method returns void, so does the overrider. Here, the method returns an int, and the overrider a std::string, which does not convert to int.

Return the type of the method, int, from the overrider. The method returns what the overrider returns, so it must be able to convert it to its own return type.

Fix

BOOST_OPENMETHOD(value, (virtual_ptr<Node>), int);

BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Plus>), int) {
    return 0;
}

If the overrider is right and the method is wrong, change the return type of the method instead.

Alternative fix

#include <string>

BOOST_OPENMETHOD(value, (virtual_ptr<Node>), std::string);

BOOST_OPENMETHOD_OVERRIDE(value, (virtual_ptr<Plus>), std::string) {
    return "+";
}

virtual_<> is not allowed in overriders

Example - Compiler Explorer

struct postfix_id;
using postfix = method<postfix_id, void(virtual_<Node&>, std::ostream&)>;

void postfix_plus(virtual_<Plus&>, std::ostream&);

BOOST_OPENMETHOD_REGISTER(postfix::override<postfix_plus>);

virtual_ marks the parameters that a method dispatches on. An overrider does not dispatch: it is called once the dispatch is done. This message comes from the core API, as above. Through BOOST_OPENMETHOD_OVERRIDE, the same mistake gives BOOST_OPENMETHOD_OVERRIDE: cannot find '…​' method that accepts the same arguments as the overrider.

Take the plain type in the overrider: Plus& where the method takes virtual_<Node&>. The method casts its argument to that type when it calls the overrider.

Fix

struct postfix_id;
using postfix = method<postfix_id, void(virtual_<Node&>, std::ostream&)>;

void postfix_plus(Plus&, std::ostream&);

BOOST_OPENMETHOD_REGISTER(postfix::override<postfix_plus>);

method parameter must be an unambiguous accessible base of corresponding overrider parameter

Example - Compiler Explorer

class Minus : Node {};

struct postfix_id;
using postfix = method<postfix_id, void(virtual_ptr<Node>, std::ostream&)>;

void postfix_minus(virtual_ptr<Minus>, std::ostream& os) {
    os << "-";
}

BOOST_OPENMETHOD_REGISTER(postfix::override<postfix_minus>);

The same mistake as in BOOST_OPENMETHOD_OVERRIDE: cannot find '…​' method that accepts the same arguments as the overrider, made with the core API, which is given the method instead of looking for it.

Make the inheritance public, as there: class Minus : public Node. The method can then cast a virtual_ptr<Node> to a virtual_ptr<Minus>.

Fix

class Minus : public Node {};

struct postfix_id;
using postfix = method<postfix_id, void(virtual_ptr<Node>, std::ostream&)>;

void postfix_minus(virtual_ptr<Minus>, std::ostream& os) {
    os << "-";
}

BOOST_OPENMETHOD_REGISTER(postfix::override<postfix_minus>);

Classes and virtual pointers

no matching function for call to 'method'

Example - Compiler Explorer

#include <iostream>

#include <boost/openmethod.hpp>
#include <boost/openmethod/initialize.hpp>

using boost::openmethod::virtual_ptr;

struct Node {};

struct Plus : Node {};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

int main() {
    boost::openmethod::initialize();

    Plus plus;
    postfix(plus, std::cout);
}
Compiler Diagnostic

gcc

no matching function for call to ‘postfix(Plus&, std::ostream&)’

clang

no matching function for call to 'postfix'

MSVC

'postfix': no matching overloaded function found

The method and the overrider compile; the call does not. An object passed as a virtual argument is converted to a virtual_ptr, which needs the dynamic type of the object. The default registry finds it with typeid, which yields the dynamic type only for a polymorphic class, so virtual_ptr does not accept Node or classes derived from it, and the call matches no overload. There is no static_assert here, because the mistake is not in a declaration that the library sees: it is the absence of a conversion. For the same reason, virtual_ptr<Node> p(plus) reports that no constructor matches.

Make the root of the hierarchy polymorphic by giving it a virtual destructor - which it needs anyway, if objects are ever deleted through a pointer to it. typeid then yields the dynamic type of a Node, and the object converts to a virtual_ptr.

Fix

#include <iostream>

#include <boost/openmethod.hpp>
#include <boost/openmethod/initialize.hpp>

using boost::openmethod::virtual_ptr;

struct Node {
    virtual ~Node() = default;
};

struct Plus : Node {};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Node>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (virtual_ptr<Plus>, std::ostream& os), void) {
    os << "+";
}

int main() {
    boost::openmethod::initialize();

    Plus plus;
    postfix(plus, std::cout);
}

Where the classes must stay non-polymorphic, there are alternatives. inplace_vptr_base gives a class a v-table pointer of its own, and a custom RTTI policy can find the dynamic type of non-polymorphic classes: see Custom RTTI.

Where the caller knows the exact class of the object, final_virtual_ptr makes a virtual_ptr without looking for the dynamic type. It takes the static type of its argument instead, so it works with any registered class, polymorphic or not:

Plus plus;
postfix(final_virtual_ptr(plus), std::cout);

Nothing checks that the static type is the dynamic type: with a non-polymorphic class, OpenMethod has no way to find out, even with runtime checks enabled. Passed a Node& that refers to a Plus, final_virtual_ptr makes a virtual_ptr to a Node, and the call dispatches on Node.


class must be an accessible unambiguous base, repeated inheritance is not supported

Example - Compiler Explorer

struct Left : Node {};
struct Right : Node {};
struct Both : Left, Right {};

BOOST_OPENMETHOD_CLASSES(Node, Left, Right, Both);

Both inherits Node twice, through Left and through Right, so a pointer to a Both cannot be converted to a pointer to a Node. A private base gives the same message.

Derive Left and Right from Node virtually. A Both then contains a single Node, and the conversion is unambiguous. If the base is private instead, make it public.

Fix

struct Left : virtual Node {};
struct Right : virtual Node {};
struct Both : Left, Right {};

BOOST_OPENMETHOD_CLASSES(Node, Left, Right, Both);

use_classes: a class is incomplete

Example - Compiler Explorer

struct Minus;

BOOST_OPENMETHOD_CLASSES(Node, Plus, Minus);

A class is registered where it is only declared.

Register it after its definition. The library needs the complete class to find its bases among the other classes of the list.

Fix

struct Minus : Node {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, Minus);

a registry must be the last element of the list

Example - Compiler Explorer

BOOST_OPENMETHOD_CLASSES(my_registry, Node, Plus);

The registry is listed before the classes. A class list may end with a registry, and only its last element is taken as one.

Move the registry to the end of the list.

Fix

BOOST_OPENMETHOD_CLASSES(Node, Plus, my_registry);

do not wrap an object that has a boost_openmethod_vptr overload in a virtual_ptr…​

Example - Compiler Explorer

#include <boost/openmethod/inplace_vptr.hpp>

struct Term : inplace_vptr_base<Term> {
    virtual ~Term() = default;
};

void print(Term& term) {
    virtual_ptr<Term> p(term);
}

The object carries its own v-table pointer, here because it derives from inplace_vptr_base. It and a virtual_ptr serve the same purpose, fast access to the v-table pointer, so wrapping one in the other is rejected.

Pass the object itself to methods: declare the parameter as virtual_<Term&>, and the method reads the v-table pointer from the object.

Fix

#include <boost/openmethod/inplace_vptr.hpp>

struct Term : inplace_vptr_base<Term> {
    virtual ~Term() = default;
};

BOOST_OPENMETHOD(postfix, (virtual_<Term&>, std::ostream&), void);

void print(Term& term) {
    postfix(term, std::cout);
}

class must inherit from inplace_vptr_base

Example - Compiler Explorer

#include <boost/openmethod/inplace_vptr.hpp>

struct Minus : Node, inplace_vptr_derived<Minus, Node> {};

The bases passed to inplace_vptr_derived are themselves inplace_vptr classes, but Node is not.

Derive the root of the hierarchy from inplace_vptr_base: here, Node derives from inplace_vptr_base<Node>. The root holds the v-table pointer, and inplace_vptr_derived sets it to the v-table of Minus.

Fix

#include <boost/openmethod/inplace_vptr.hpp>

struct Node : inplace_vptr_base<Node> {
    virtual ~Node() = default;
};

struct Minus : Node, inplace_vptr_derived<Minus, Node> {};

registry can be specified only for root classes

Example - Compiler Explorer

#include <boost/openmethod/inplace_vptr.hpp>

struct Term : inplace_vptr_base<Term> {};

struct Sum : Term, inplace_vptr_derived<Sum, Term, my_registry> {};

Sum names a registry, but it is not the root of its inplace_vptr hierarchy.

Name the registry at the root instead, as in inplace_vptr_base<Term, my_registry>. Its derived classes inherit it.

Fix

#include <boost/openmethod/inplace_vptr.hpp>

struct Term : inplace_vptr_base<Term, my_registry> {};

struct Sum : Term, inplace_vptr_derived<Sum, Term> {};

the rtti policy deems this class polymorphic, so it must provide dynamic_type

Example - Compiler Explorer

struct my_rtti : policies::rtti {
    template<class Registry>
    struct fn : defaults {
        template<class T>
        static constexpr bool is_polymorphic = std::is_polymorphic_v<T>;

        template<class T>
        static auto static_type() -> type_id {
            return &typeid(T);
        }
    };
};

struct rtti_registry : default_registry::with<my_rtti> {};

void print(Node& node) {
    virtual_ptr<Node, rtti_registry> p(node);
}

A custom rtti policy that reports a class as polymorphic must also be able to find the dynamic type of its objects. See Custom RTTI.

Give the policy a dynamic_type function that returns the type_id of the dynamic type of its argument, or make is_polymorphic false for that class. Here, dynamic_type uses typeid, which matches is_polymorphic.

Fix

struct my_rtti : policies::rtti {
    template<class Registry>
    struct fn : defaults {
        template<class T>
        static constexpr bool is_polymorphic = std::is_polymorphic_v<T>;

        template<class T>
        static auto static_type() -> type_id {
            return &typeid(T);
        }

        template<class T>
        static auto dynamic_type(const T& obj) -> type_id {
            return &typeid(obj);
        }
    };
};

struct rtti_registry : default_registry::with<my_rtti> {};

void print(Node& node) {
    virtual_ptr<Node, rtti_registry> p(node);
}

the rtti policy does not deem this class polymorphic

Example - Compiler Explorer

struct Literal {};

void print(Literal& literal) {
    default_registry::policy<policies::vptr>::dynamic_vptr(literal);
}

The converse. The library never asks for the dynamic type of a class that its rtti policy does not deem polymorphic, so this message only comes from code that calls the vptr policy directly, or from a custom virtual_traits.

Ask for the dynamic type only of a class that the rtti policy deems polymorphic. With the default registry, that is a class with a virtual function: here, give Literal a virtual destructor.

Fix

struct Literal {
    virtual ~Literal() = default;
};

void print(Literal& literal) {
    default_registry::policy<policies::vptr>::dynamic_vptr(literal);
}

Registries

See Registry Affinity for how a class, a parameter and a method come to belong to a registry.


registry mismatch

Example - Compiler Explorer

BOOST_OPENMETHOD(
    postfix, (virtual_ptr<Node>, std::ostream&), void, my_registry);

A virtual_ptr, an overrider or a virtual_any belongs to a registry other than the method’s. Here, virtual_ptr<Node> belongs to the default registry, and the method to my_registry.

Put the parameter in the registry of the method: name the registry in the virtual_ptr, as below, or declare an affinity for my_registry in Node, which makes virtual_ptr<Node> belong to it.

Fix

BOOST_OPENMETHOD(
    postfix, (virtual_ptr<Node, my_registry>, std::ostream&), void,
    my_registry);

registry mismatch: the parameter belongs to another registry

Example - Compiler Explorer

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD(
    postfix, (virtual_<Expr&>, std::ostream&), void, default_registry);

The class of a virtual parameter declares an affinity for a registry, or the parameter names one, and the method names another.

Name the registry that the class declares on the method.

Fix

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD(postfix, (virtual_<Expr&>, std::ostream&), void, my_registry);

Or name none: a method that names no registry takes the one its parameters carry.

Alternative fix

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD(postfix, (virtual_<Expr&>, std::ostream&), void);

registry mismatch: a class declares an affinity for another registry

Example - Compiler Explorer

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

struct Sum : Expr {};

BOOST_OPENMETHOD_CLASSES(Expr, Sum, default_registry);

A class list names a registry, and one of the classes declares an affinity for another.

List the registry that the classes declare.

Fix

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

struct Sum : Expr {};

BOOST_OPENMETHOD_CLASSES(Expr, Sum, my_registry);

Or list none: a list that names no registry takes the one its classes agree on.

Alternative fix

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

struct Sum : Expr {};

BOOST_OPENMETHOD_CLASSES(Expr, Sum);

virtual parameters carry conflicting registries

Example - Compiler Explorer

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD(
    print, (virtual_ptr<Node>, virtual_ptr<Expr>, std::ostream&), void);

The method names no registry, and its virtual parameters belong to different ones: Node to the default registry, Expr to my_registry.

Make the parameters agree: virtual_ptr<Node, my_registry>. Naming the registry on the method is not enough here: a virtual_ptr always carries a registry, so virtual_ptr<Node> would then give registry mismatch.

Fix

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD(
    print, (virtual_ptr<Node, my_registry>, virtual_ptr<Expr>, std::ostream&),
    void);

Or virtual_<Node&>, which adopts the registry of the method because Node declares none.

Alternative fix

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD(
    print, (virtual_<Node&>, virtual_ptr<Expr>, std::ostream&), void);

the classes carry conflicting registry affinities - list the registry to say which one is meant

Example - Compiler Explorer

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD_CLASSES(Node, Plus, Expr);

A class list mixes classes that declare different affinities, or, as here, classes that declare one with classes that declare none.

Here, Node and Plus belong to the default registry, and Expr to my_registry. Register them in separate lists: each list then takes the registry its classes agree on.

Fix

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD_CLASSES(Node, Plus);
BOOST_OPENMETHOD_CLASSES(Expr);

When all the classes are meant for the same registry, add it at the end of the list instead.

Alternative fix

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD_CLASSES(Node, Plus, Expr, my_registry);

cannot tell which registry this class belongs to…​

Example - Compiler Explorer

struct Expr {
    virtual ~Expr() = default;
    friend auto boost_openmethod_registry(Expr*) -> my_registry;
};

struct Term {
    virtual ~Term() = default;
    friend auto boost_openmethod_registry(Term*) -> default_registry;
};

struct Sum : Expr, Term {};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Sum>, std::ostream&), void);

The class inherits different affinities from several bases, or inherits one from a private base.

Declare the affinity in the class itself. Its own declaration takes precedence over the ones it inherits.

Fix

struct Expr {
    virtual ~Expr() = default;
    friend auto boost_openmethod_registry(Expr*) -> my_registry;
};

struct Term {
    virtual ~Term() = default;
    friend auto boost_openmethod_registry(Term*) -> default_registry;
};

struct Sum : Expr, Term {
    friend auto boost_openmethod_registry(Sum*) -> my_registry;
};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Sum>, std::ostream&), void);

boost_openmethod_registry must return a registry

Example - Compiler Explorer

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = int;
};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Expr>, std::ostream&), void);

The affinity declaration names a type that is not a registry.

Name a registry: a class derived from registry, such as default_registry or my_registry.

Fix

struct Expr {
    virtual ~Expr() = default;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Expr>, std::ostream&), void);

this class declared its registry affinity after it was first mentioned…​

Example - Compiler Explorer

struct Expr {
    virtual ~Expr() = default;
    virtual_ptr<Expr> next;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Expr>, std::ostream&), void);

virtual_ptr<Expr> was formed - here, inside Expr itself - before the affinity declaration was seen, and kept the default registry.

Move the declaration up, to the top of the class, so that it precedes every use of the class.

Fix

struct Expr {
    using boost_openmethod_registry = my_registry;
    virtual ~Expr() = default;
    virtual_ptr<Expr> next;
};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Expr>, std::ostream&), void);

Or name the registry where the class is first used.

Alternative fix

struct Expr {
    virtual ~Expr() = default;
    virtual_ptr<Expr, my_registry> next;
    using boost_openmethod_registry = my_registry;
};

BOOST_OPENMETHOD(postfix, (virtual_ptr<Expr>, std::ostream&), void);

Smart pointers

std::shared_ptr cannot be passed by non-const lvalue reference

Example - Compiler Explorer

#include <boost/openmethod/interop/std_shared_ptr.hpp>

BOOST_OPENMETHOD(postfix, (shared_virtual_ptr<Node>&, std::ostream&), void);

A method may have to build the smart pointer from its argument, for example a shared_virtual_ptr<Node> from a std::shared_ptr<Plus>. The result is a temporary, which a non-const lvalue reference cannot bind to.

Pass a std::shared_ptr, or a shared_virtual_ptr, by value or by const reference.

Fix

#include <boost/openmethod/interop/std_shared_ptr.hpp>

BOOST_OPENMETHOD(postfix, (shared_virtual_ptr<Node>, std::ostream&), void);

Or by const reference.

Alternative fix

#include <boost/openmethod/interop/std_shared_ptr.hpp>

BOOST_OPENMETHOD(
    postfix, (const shared_virtual_ptr<Node>&, std::ostream&), void);

cast only allowed to a shared_ptr

Example - Compiler Explorer

#include <boost/openmethod/interop/std_shared_ptr.hpp>

BOOST_OPENMETHOD(
    postfix, (virtual_<const std::shared_ptr<Node>&>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (std::shared_ptr<Plus>& plus, std::ostream& os), void) {
    os << "+";
}

The method takes a std::shared_ptr, and the overrider something else in the same position: here, a non-const reference.

The overrider takes a std::shared_ptr too, in the same form as the method: const std::shared_ptr<Plus>&. The method casts its argument to a new std::shared_ptr<Plus>, which a non-const reference cannot bind to.

Fix

#include <boost/openmethod/interop/std_shared_ptr.hpp>

BOOST_OPENMETHOD(
    postfix, (virtual_<const std::shared_ptr<Node>&>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(
    postfix, (const std::shared_ptr<Plus>&, std::ostream& os), void) {
    os << "+";
}

a std::weak_ptr cannot be wrapped in a virtual_ptr; use weak_virtual_ptr

Example - Compiler Explorer

#include <boost/openmethod/interop/std_weak_ptr.hpp>

void watch(std::weak_ptr<Node> node) {
    virtual_ptr<std::weak_ptr<Node>> p(node);
}

A virtual_ptr refers to an object that exists, so that a method can dispatch on it. A std::weak_ptr may outlive its object.

Use weak_virtual_ptr to keep a weak reference together with its v-table pointer. Its lock() returns a shared_virtual_ptr, which can be passed to a method.

Fix

#include <boost/openmethod/interop/std_weak_ptr.hpp>

void watch(std::weak_ptr<Node> node) {
    weak_virtual_ptr<Node> p(node);
}

a weak pointer cannot be a virtual parameter; call lock() first

Example - Compiler Explorer

#include <boost/openmethod/interop/std_weak_ptr.hpp>

BOOST_OPENMETHOD(postfix, (virtual_<std::weak_ptr<Node>>, std::ostream&), void);

A method cannot dispatch on an object that may no longer exist.

Make the parameter a std::shared_ptr. Lock the weak pointer before the call, and pass the resulting shared pointer if the object still exists.

Fix

#include <boost/openmethod/interop/std_shared_ptr.hpp>

BOOST_OPENMETHOD(
    postfix, (virtual_<std::shared_ptr<Node>>, std::ostream&), void);

void call(const std::weak_ptr<Node>& node) {
    if (auto locked = node.lock()) {
        postfix(locked, std::cout);
    }
}

any and type erasure


requires standard RTTI

Example - Compiler Explorer

#include <boost/openmethod/policies/static_rtti.hpp>
#include <boost/openmethod/interop/std_any.hpp>

struct static_registry : default_registry::with<policies::static_rtti> {};

BOOST_OPENMETHOD(
    postfix, (virtual_<const std::any&>, std::ostream&), void, static_registry);

Dispatching on the content of an any keys on its std::type_info, so the registry uses std_rtti, or a policy derived from it.

Declare the method in a registry that uses std_rtti, here the default registry. If the rest of the program needs static_rtti, keep the methods that dispatch on an any in a registry of their own: registries are independent of each other.

Fix

#include <boost/openmethod/interop/std_any.hpp>

BOOST_OPENMETHOD(postfix, (virtual_<const std::any&>, std::ostream&), void);

virtual_any must be passed by reference

Example - Compiler Explorer

#include <boost/openmethod/interop/std_any.hpp>

BOOST_OPENMETHOD(postfix, (virtual_std_any, std::ostream&), void);

A virtual_any passed by value would copy the any, and its content, on every call.

Pass it by reference: const virtual_std_any&.

Fix

#include <boost/openmethod/interop/std_any.hpp>

BOOST_OPENMETHOD(postfix, (const virtual_std_any&, std::ostream&), void);

Or virtual_std_any&, if the overriders modify the content.

Alternative fix

#include <boost/openmethod/interop/std_any.hpp>

BOOST_OPENMETHOD(postfix, (virtual_std_any&, std::ostream&), void);

an owning type_erasure::any must be passed by reference

Example - Compiler Explorer

#include <boost/mpl/vector.hpp>
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/builtin.hpp>
#include <boost/openmethod/interop/boost_type_erasure.hpp>

namespace te = boost::type_erasure;

using any_node =
    te::any<boost::mpl::vector<te::copy_constructible<>, te::relaxed>>;

BOOST_OPENMETHOD(postfix, (virtual_<any_node>, std::ostream&), void);

The same, for a Boost.TypeErasure any that owns its value.

Pass it by reference, as a std::any: virtual_<const any_node&>.

Fix

#include <boost/mpl/vector.hpp>
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/builtin.hpp>
#include <boost/openmethod/interop/boost_type_erasure.hpp>

namespace te = boost::type_erasure;

using any_node =
    te::any<boost::mpl::vector<te::copy_constructible<>, te::relaxed>>;

BOOST_OPENMETHOD(postfix, (virtual_<const any_node&>, std::ostream&), void);

Or dispatch on a reference form, any<C, _self&> or any<C, const _self&>. These are handles that do not own their value, and may be passed by value.

Alternative fix

#include <boost/mpl/vector.hpp>
#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/builtin.hpp>
#include <boost/openmethod/interop/boost_type_erasure.hpp>

namespace te = boost::type_erasure;

using any_node_ref = te::any<
    boost::mpl::vector<te::copy_constructible<>, te::relaxed>,
    const te::_self&>;

BOOST_OPENMETHOD(postfix, (virtual_<any_node_ref>, std::ostream&), void);

do not wrap a virtual_any in a virtual_ptr…​

Example - Compiler Explorer

#include <boost/openmethod/interop/std_any.hpp>

void print(virtual_std_any& node) {
    virtual_ptr<virtual_std_any> p(node);
}

A virtual_any already carries the v-table pointer of its content.

Pass the virtual_any itself, by reference, to a method that takes a virtual_std_any&. It plays the part that a virtual_ptr plays for a class.

Fix

#include <boost/openmethod/interop/std_any.hpp>

BOOST_OPENMETHOD(postfix, (virtual_std_any&, std::ostream&), void);

void print(virtual_std_any& node) {
    postfix(node, std::cout);
}

use of deleted function …​ final_virtual_ptr

Example - Compiler Explorer

#include <boost/openmethod/interop/std_any.hpp>

void print(std::any& node) {
    final_virtual_ptr(node);
}

final_virtual_ptr would use the v-table of the any class itself, not that of its content, so the overloads for any types are deleted. Clang says call to deleted function, and MSVC attempting to reference a deleted function.

Hold the object in a virtual_std_any instead of a std::any, and pass it by reference to a method that takes a virtual_std_any&. It acquires the v-table pointer of its content once, when it is built - without a lookup, when it is built from a value - and the method uses that pointer as it is.

Fix

#include <boost/openmethod/interop/std_any.hpp>

BOOST_OPENMETHOD(postfix, (virtual_std_any&, std::ostream&), void);

void print(virtual_std_any& node) {
    postfix(node, std::cout);
}

no matching function for call to …​cast

Example - Compiler Explorer

#include <boost/openmethod/interop/boost_any.hpp>

BOOST_OPENMETHOD(postfix, (virtual_<const boost::any&>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(postfix, (Plus&, std::ostream& os), void) {
    os << "+";
}

The overrider takes the content of the any by a kind of reference that the any cannot provide: a mutable reference to the content of a const any, as here, or an rvalue reference to the content of an lvalue any. MSVC says no matching overloaded function found.

Take the content by a reference that the any provides: here, const Plus&, since the method takes a const boost::any&.

Fix

#include <boost/openmethod/interop/boost_any.hpp>

BOOST_OPENMETHOD(postfix, (virtual_<const boost::any&>, std::ostream&), void);

BOOST_OPENMETHOD_OVERRIDE(postfix, (const Plus&, std::ostream& os), void) {
    os << "+";
}

Policies


the `state` of a policy that defines `initialize` must be copyable...

Example - Compiler Explorer

#include <memory>
#include <boost/openmethod/initialize.hpp>

struct cache_policy {
    using category = cache_policy;

    template<class Registry>
    struct fn {
        struct state {
            std::unique_ptr<int> data;
        };

        template<class Context, class... Options>
        static void initialize(const Context&, const std::tuple<Options...>&) {
        }
    };
};

struct cached_registry : default_registry::with<cache_policy> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, cached_registry);

void init() {
    initialize<cached_registry>();
}

initialize saves the state of each policy that has an initialize function, and restores it if another policy throws. A std::unique_ptr member makes it impossible to copy.

Hold the data in a std::shared_ptr, so that the state can be copied.

Fix

#include <memory>
#include <boost/openmethod/initialize.hpp>

struct cache_policy {
    using category = cache_policy;

    template<class Registry>
    struct fn {
        struct state {
            std::shared_ptr<int> data;
        };

        template<class Context, class... Options>
        static void initialize(const Context&, const std::tuple<Options...>&) {
        }
    };
};

struct cached_registry : default_registry::with<cache_policy> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, cached_registry);

void init() {
    initialize<cached_registry>();
}

Or hold the data by value.

Alternative fix

#include <boost/openmethod/initialize.hpp>

struct cache_policy {
    using category = cache_policy;

    template<class Registry>
    struct fn {
        struct state {
            int data = 0;
        };

        template<class Context, class... Options>
        static void initialize(const Context&, const std::tuple<Options...>&) {
        }
    };
};

struct cached_registry : default_registry::with<cache_policy> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, cached_registry);

void init() {
    initialize<cached_registry>();
}

the `state` of a policy that defines `initialize` must be nothrow move-assignable...

Example - Compiler Explorer

#include <boost/openmethod/initialize.hpp>

struct cache_policy {
    using category = cache_policy;

    template<class Registry>
    struct fn {
        struct state {
            state() = default;
            state(const state&) = default;
            auto operator=(const state&) -> state& = default;

            auto operator=(state&&) -> state& {
                return *this;
            }
        };

        template<class Context, class... Options>
        static void initialize(const Context&, const std::tuple<Options...>&) {
        }
    };
};

struct cached_registry : default_registry::with<cache_policy> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, cached_registry);

void init() {
    initialize<cached_registry>();
}

The state is restored from a destructor, while an exception is in flight, where throwing would terminate the program.

Declare the move assignment noexcept.

Fix

#include <boost/openmethod/initialize.hpp>

struct cache_policy {
    using category = cache_policy;

    template<class Registry>
    struct fn {
        struct state {
            state() = default;
            state(const state&) = default;
            auto operator=(const state&) -> state& = default;

            auto operator=(state&&) noexcept -> state& {
                return *this;
            }
        };

        template<class Context, class... Options>
        static void initialize(const Context&, const std::tuple<Options...>&) {
        }
    };
};

struct cached_registry : default_registry::with<cache_policy> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, cached_registry);

void init() {
    initialize<cached_registry>();
}

Or default the move assignment, and let the compiler generate it: a generated one is noexcept when those of the members are.

Alternative fix

#include <boost/openmethod/initialize.hpp>

struct cache_policy {
    using category = cache_policy;

    template<class Registry>
    struct fn {
        struct state {
            state() = default;
            state(const state&) = default;
            auto operator=(const state&) -> state& = default;
            auto operator=(state&&) -> state& = default;
        };

        template<class Context, class... Options>
        static void initialize(const Context&, const std::tuple<Options...>&) {
        }
    };
};

struct cached_registry : default_registry::with<cache_policy> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, cached_registry);

void init() {
    initialize<cached_registry>();
}

two or more stateful policies in this registry share the same nested state type…​

Example - Compiler Explorer

struct counter {
    int n = 0;
};

struct calls_policy {
    using category = calls_policy;

    template<class Registry>
    struct fn {
        using state = counter;
    };
};

struct errors_policy {
    using category = errors_policy;

    template<class Registry>
    struct fn {
        using state = counter;
    };
};

struct counting_registry :
    default_registry::with<calls_policy, errors_policy> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, counting_registry);

The registry keeps the states of its policies together, and finds each by its type.

Give each policy a state type of its own, for example a nested struct. Here, each policy derives its state from counter, which keeps the members in one place.

Fix

struct counter {
    int n = 0;
};

struct calls_policy {
    using category = calls_policy;

    template<class Registry>
    struct fn {
        struct state : counter {};
    };
};

struct errors_policy {
    using category = errors_policy;

    template<class Registry>
    struct fn {
        struct state : counter {};
    };
};

struct counting_registry :
    default_registry::with<calls_policy, errors_policy> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, counting_registry);

the type_hash policy must appear before vptr_vector in the registry's policy_list

Example - Compiler Explorer

#include <boost/openmethod/policies/minimal_perfect_hash.hpp>

struct hashed_registry :
    default_registry::without<policies::type_hash>::with<
        policies::minimal_perfect_hash<>> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, hashed_registry);

vptr_vector uses the hash computed by the type_hash policy, so it comes after it in the registry’s policy list. without followed by with appends the policy at the end.

Use with alone. It replaces a policy in place, so default_registry::with<policies::minimal_perfect_hash<>> puts the new hash where fast_perfect_hash was, ahead of vptr_vector.

Fix

#include <boost/openmethod/policies/minimal_perfect_hash.hpp>

struct hashed_registry :
    default_registry::with<policies::minimal_perfect_hash<>> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, hashed_registry);

minimal_cover_hash needs BMI2…​

Example - Compiler Explorer

#include <boost/openmethod/policies/minimal_cover_hash.hpp>

struct covered_registry :
    default_registry::with<policies::minimal_cover_hash<>> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, covered_registry);

minimal_cover_hash uses the x86-64 PEXT instruction, and the translation unit is compiled for a processor that may not have it. MSVC does not report the error for x64 builds: it compiles the instruction whatever the flags, so the program requires a processor that supports it.

Compile every translation unit with -mbmi2, or with a -march that implies it, such as -march=haswell.

Fix

target_compile_options(my_app PRIVATE -mbmi2)

If the program must also run on processors without BMI2, or on other architectures, use minimal_perfect_hash instead, which is portable.

Alternative fix

#include <boost/openmethod/policies/minimal_perfect_hash.hpp>

struct hashed_registry :
    default_registry::with<policies::minimal_perfect_hash<>> {};

BOOST_OPENMETHOD_CLASSES(Node, Plus, hashed_registry);

C++26 reflection

The examples need a compiler that supports reflection, such as GCC 16 with -std=c++26 -freflection.


a group holds reflections of namespaces, classes or registries

Example - Compiler Explorer

BOOST_OPENMETHOD_REGISTER(register_classes<{^^int}>);

A group passed to register_classes contains a reflection of something else, such as a fundamental type, an enumeration or a function.

List only namespaces, classes and registries: here, the classes Node and Plus.

Fix

BOOST_OPENMETHOD_REGISTER(register_classes<{^^Node, ^^Plus}>);

a group holds one kind of reflection…​

Example - Compiler Explorer

BOOST_OPENMETHOD_REGISTER(register_classes<{^^Node, ^^Plus, ^^my_registry}>);

A group mixes reflections of different kinds: here, two classes and a registry.

Put the namespaces, the classes and the registries in groups of their own.

Fix

BOOST_OPENMETHOD_REGISTER(register_classes<{^^Node, ^^Plus}, {^^my_registry}>);

order the groups as namespaces, classes, registries

Example - Compiler Explorer

BOOST_OPENMETHOD_REGISTER(register_classes<{^^my_registry}, {^^Node, ^^Plus}>);

The groups passed to register_classes are out of order: here, the registry comes before the classes.

Pass the namespaces first, then the classes, then the registries.

Fix

BOOST_OPENMETHOD_REGISTER(register_classes<{^^Node, ^^Plus}, {^^my_registry}>);

a listed class must be complete

Example - Compiler Explorer

struct Minus;

BOOST_OPENMETHOD_REGISTER(register_classes<{^^Node, ^^Minus}>);

A class listed explicitly is only declared, not defined.

Register the class after its definition.

Fix

struct Minus : Node {};

BOOST_OPENMETHOD_REGISTER(register_classes<{^^Node, ^^Minus}>);

Platform

These two do not depend on the program, so they come without an example.


This implementation requires that size_t and void* have the same size.

The platform is not supported. No x86, x86-64, ARM or AArch64 target is affected.

There is no fix in the program. OpenMethod reads type ids as unsigned integers, so it needs one as wide as a pointer: std::uintptr_t, or std::size_t where the standard library does not provide std::uintptr_t. A target that has neither cannot use the library.


named_static_rtti requires __PRETTY_FUNCTION__ or __FUNCSIG__

named_static_rtti derives class names from the predefined function name of GCC, Clang or MSVC, and the compiler provides neither.

Use static_rtti instead, if the program is a single module. The names are what lets named_static_rtti merge the copies of a class that come from different modules; static_rtti identifies classes without them, but its type ids differ from one module to the next. A program that shares classes across shared libraries needs std_rtti, or a custom RTTI policy: see Custom RTTI.

Runtime errors

Errors detected at run time are passed to the registry’s error_handler policy. The one in default_registry writes the message to stderr, then the program is terminated with abort. Error Handling explains how to change that, for example to throw an exception. Each section below names the error type that a custom handler receives.

initialize always checks the classes, methods and overriders it is given, and a call that finds no overrider, or several ambiguous ones, is always reported. The other checks made during a call - for an unregistered class, a call before initialize, a misused final_virtual_ptr - are made only if the registry contains the runtime_checks policy. default_registry contains it if BOOST_OPENMETHOD_ENABLE_RUNTIME_CHECKS is defined, which is recommended in debug builds.

Registration

unknown class C

Error type: missing_class.

Every class that appears in a virtual parameter, in a method or in an overrider, must be registered with BOOST_OPENMETHOD_CLASSES. So must the class of every object passed as a virtual argument. initialize detects the first two omissions:

Example - Compiler Explorer

BOOST_OPENMETHOD_CLASSES(Dog); // Animal is missing

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

// aborts with error message: unknown class Animal
initialize();

Example - Compiler Explorer

BOOST_OPENMETHOD_CLASSES(Animal); // Dog is missing

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

// aborts with error message: unknown class Dog
initialize();

The third one can be detected only during the call, and only if runtime checks are enabled. Otherwise, the call reads a v-table pointer that was never set up, and the behavior is undefined: it may call the wrong overrider, or crash.

Example - Compiler Explorer

// Bulldog is missing
BOOST_OPENMETHOD_CLASSES(Animal, Dog);

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

Bulldog hector;

// aborts with error message: unknown class Bulldog
poke(hector);

Register every class, in a BOOST_OPENMETHOD_CLASSES that also lists its direct bases. initialize then knows the class and its place in the hierarchy, and sets up its v-table. Listing Animal, Dog and Bulldog together fixes all three examples.

Fix

BOOST_OPENMETHOD_CLASSES(Animal, Dog, Bulldog);

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

Bulldog hector;
poke(hector);

missing base B -<| D

Error type: missing_base.

initialize learns about inheritance relationships from the class registrations: each lists a class together with its direct bases. Two classes that are registered separately are unrelated, as far as OpenMethod is concerned. If an overrider implies that one derives from the other, initialize reports it:

Example - Compiler Explorer

// registered separately, so the inheritance is never seen
BOOST_OPENMETHOD_CLASSES(Animal);
BOOST_OPENMETHOD_CLASSES(Dog);

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

// aborts with error message: missing base Animal -<| Dog
initialize();

The message names the base class first, then the derived class.

Register the two classes in the same BOOST_OPENMETHOD_CLASSES. A class list tells initialize which of its classes derive from which, so it learns that Dog derives from Animal.

Fix

BOOST_OPENMETHOD_CLASSES(Animal, Dog);

Dispatch

not implemented

Error type: no_overrider.

No overrider is applicable to the dynamic types of the virtual arguments:

Example - Compiler Explorer

BOOST_OPENMETHOD_CLASSES(Animal, Dog, Cat);

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

Cat felix;

// aborts with error message: not implemented
poke(felix);

This is not a mistake by itself: a method may well be meant for some classes only. See Finding the calls that cannot be dispatched to find out, at initialize time, whether any call can end like this.

Add an overrider for Animal. It serves as a default: it is called for every class that has no more specialized overrider of its own.

Fix

BOOST_OPENMETHOD_CLASSES(Animal, Dog, Cat);

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Animal>), void) {
    // ...
}

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

Cat felix;
poke(felix);

Or add one for Cat. It covers Cat alone: an Animal that is neither a Dog nor a Cat, or a class added later, still has no overrider.

Alternative fix

BOOST_OPENMETHOD_CLASSES(Animal, Dog, Cat);

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Cat>), void) {
    // ...
}

Cat felix;
poke(felix);

ambiguous

Error type: ambiguous_call.

Several overriders are applicable, and none of them is more specialized than all the others:

Example - Compiler Explorer

BOOST_OPENMETHOD_CLASSES(Animal, Dog);

BOOST_OPENMETHOD(meet, (virtual_ptr<Animal>, virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(meet, (virtual_ptr<Animal>, virtual_ptr<Dog>), void) {
    // ...
}

BOOST_OPENMETHOD_OVERRIDE(meet, (virtual_ptr<Dog>, virtual_ptr<Animal>), void) {
    // ...
}

Dog snoopy, hector;

// aborts with error message: ambiguous
meet(snoopy, hector);

Both overriders accept two Dogs, and each is more specialized than the other in one argument. See Multiple Dispatch.

Add an overrider for (Dog, Dog). It is more specialized than both of the others, so it is the one called for two Dogs.

Fix

BOOST_OPENMETHOD_CLASSES(Animal, Dog);

BOOST_OPENMETHOD(meet, (virtual_ptr<Animal>, virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(meet, (virtual_ptr<Animal>, virtual_ptr<Dog>), void) {
    // ...
}

BOOST_OPENMETHOD_OVERRIDE(meet, (virtual_ptr<Dog>, virtual_ptr<Animal>), void) {
    // ...
}

BOOST_OPENMETHOD_OVERRIDE(meet, (virtual_ptr<Dog>, virtual_ptr<Dog>), void) {
    // ...
}

Dog snoopy, hector;
meet(snoopy, hector);

Finding the calls that cannot be dispatched

initialize already knows about every combination of classes that ends in not implemented or ambiguous, and says how many there are in its report. A program can check it, and set the BOOST_OPENMETHOD_TRACE environment variable to 1 to find out which methods and classes are involved:

auto report = bom::initialize(bom::trace::from_env()).report;

if (report.not_implemented != 0 || report.ambiguous) {
    std::cerr << "some methods are ambiguous or not implemented for "
                 "some combinations of virtual arguments\n"
                 "set BOOST_OPENMETHOD_TRACE=1 to troubleshoot\n";
    exit(1);
}

The trace lists the classes, the methods, their overriders and the dispatch tables that initialize builds from them. Its format is not specified, and may change from one version to the next.

Calls

not initialized

Error type: not_initialized.

A method was called before initialize:

Example - Compiler Explorer

BOOST_OPENMETHOD_CLASSES(Animal, Dog);

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

// initialize() has not been called
Dog snoopy;

// aborts with error message: not initialized
poke(snoopy);

This is detected only with runtime checks; otherwise, the behavior is undefined.

Call initialize before the first method call, typically at the start of main: it builds the dispatch tables that the call reads. A shared library loaded at run time adds classes, methods and overriders, so call it again after loading one. See Shared Libraries.

Fix

BOOST_OPENMETHOD_CLASSES(Animal, Dog);

BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>), void);

BOOST_OPENMETHOD_OVERRIDE(poke, (virtual_ptr<Dog>), void) {
    // ...
}

initialize();

Dog snoopy;
poke(snoopy);

invalid call to final construct: static type = S, dynamic type = D

Error type: final_error.

final_virtual_ptr takes the static type of its argument for its dynamic type, without checking it, unless runtime checks are enabled:

Example - Compiler Explorer

Dog snoopy;
Animal& animal = snoopy;

// aborts with error message:
// invalid call to final construct: static type = Animal, dynamic type = Dog
poke(final_virtual_ptr(animal));

Pass final_virtual_ptr an object whose static type is exactly its dynamic type - here, snoopy itself - or use virtual_ptr, which finds the dynamic type.

Fix

Dog snoopy;
Animal& animal = snoopy;

poke(final_virtual_ptr(snoopy));
poke(virtual_ptr(animal));

Builds

conflicting definitions of R

Error type: odr_violation.

BOOST_OPENMETHOD_ENABLE_RUNTIME_CHECKS changes the definition of default_registry, so it must be defined in all the translation units of the program, or in none. When it is defined in some and not in others, initialize reports it before anything else:

Example

// aborts with error message:
// conflicting definitions of boost::openmethod::default_registry
initialize();

Define the symbol in the build system, for every translation unit, rather than in source files. In CMake, add_compile_definitions in the top-level CMakeLists.txt, before any add_subdirectory, applies to every target of the project. This one defines the symbol in debug builds only.

Fix

add_compile_definitions(
    $<$<CONFIG:Debug>:BOOST_OPENMETHOD_ENABLE_RUNTIME_CHECKS>)

Hash tables

The type_hash policy maps type ids to the indexes of the v-tables. Its initialize searches for a function that does that for the registered classes, and reports an error if it cannot find one. Each policy has its own:

Message Policy

could not find hash factors after N attempts using up to M buckets

fast_perfect_hash, the default

could not place N type ids after trying M multipliers

minimal_perfect_hash

could not place N type ids in a table of up to 2^B slots

two_level_hash

the smallest bit cover of N type ids is B bits wide, more than the M allowed

minimal_cover_hash

With the default limits, the first three are unlikely with ordinary type ids, and cannot be provoked by a small program. The last two policies take their limits as template arguments, so it takes a registry configured to fail:

Example - Compiler Explorer

struct hashed_registry
    : default_registry::with<policies::minimal_perfect_hash<4, 95, 0>> {};

BOOST_OPENMETHOD_CLASSES(Animal, Dog, hashed_registry);

// aborts with error message:
// could not place 2 type ids after trying 0 multipliers
initialize<hashed_registry>();

Example - Compiler Explorer

struct covered_registry
    : default_registry::with<policies::minimal_cover_hash<0>> {};

BOOST_OPENMETHOD_CLASSES(Animal, Dog, covered_registry);

// aborts with error message:
// the smallest bit cover of 2 type ids is 1 bits wide, more than the 0 allowed
initialize<covered_registry>();

The type ids that a policy fails on usually come from a custom RTTI policy.

Raise the limit: MaxSeeds, the third template argument of minimal_perfect_hash, or MaxBits, the only one of minimal_cover_hash. Here, their defaults, 16 and 24, are enough. Or use another type_hash policy: see Type ids across modules for how the policies compare.

Fix

struct hashed_registry :
    default_registry::with<policies::minimal_perfect_hash<4, 95, 16>> {};

BOOST_OPENMETHOD_CLASSES(Animal, Dog, hashed_registry);

initialize<hashed_registry>();