BOOST_OPENMETHOD_DECLARE_OVERRIDER

Declare a method overrider.

Synopsis

Declared in <boost/openmethod/macros.hpp>

#define BOOST_OPENMETHOD_DECLARE_OVERRIDER(ID, PARAMETERS, …​)

Description

Declares an overrider for a method, but does not start its definition. This macro can be used in header files.

ID is the identifier of the method to which the overrider is added.

PARAMETERS is a comma‐separated list of types, possibly followed by parameter names, just like in a function declaration.

The macro tries to locate a method that can be called with the same argument list as the overrider, possibly via argument dependent lookup.

Each virtual_ptr<T> in the method's parameter list must have a corresponding virtual_ptr<U> parameter in the same position in the overrider's parameter list, such that U is the same as T, or has T as an accessible unambiguous base.

Each virtual_<T> in the method's parameter list must have a corresponding U parameter in the same position in the overrider's parameter list, such that U is the same as T, or has T as an accessible unambiguous base.

ID must be an identifier. Qualified names are not allowed.

Example

Use this macro, rather than BOOST_OPENMETHOD_OVERRIDE, to split an overrider across a header and an implementation file. The header declares the overrider without a body:

// roles.hpp

#ifndef ROLES_HPP
#define ROLES_HPP

#include <boost/openmethod.hpp>

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

struct Salesman : Employee {
    double sales = 0.0;
};

BOOST_OPENMETHOD(pay, (boost::openmethod::virtual_ptr<const Employee>), double);

BOOST_OPENMETHOD_DECLARE_OVERRIDER(
    pay, (boost::openmethod::virtual_ptr<const Employee>), double);

#endif // ROLES_HPP

The implementation file supplies the body with BOOST_OPENMETHOD_DEFINE_OVERRIDER

#include "roles.hpp"
#include <boost/openmethod.hpp>

BOOST_OPENMETHOD_DEFINE_OVERRIDER(
    pay, (boost::openmethod::virtual_ptr<const Employee>), double) {
    return 5000.0;
}

BOOST_OPENMETHOD_CLASSES(Employee);

This specific overrider can be called from other overriders explictly. No dynamic dispatch is performed.

BOOST_OPENMETHOD_OVERRIDE(
    pay, (boost::openmethod::virtual_ptr<const Salesman> emp), double) {
    return BOOST_OPENMETHOD_OVERRIDER(
               pay, (boost::openmethod::virtual_ptr<const Employee> emp),
               double)::fn(emp) +
        emp->sales * 0.05; // base + commission
}

Implementation Notes

The macro creates additional entities in the current scope.

  • A class template declaration that acts as a container for the method's overriders in the current scope:

template<typename...> struct BOOST_OPENMETHOD_OVERRIDERS(ID);
  • A specialization of the container for the overrider:

struct BOOST_OPENMETHOD_OVERRIDERS(ID)<RETURN_TYPE(PARAMETERS...)> {
    static auto fn(PARAMETERS...) -> RETURN_TYPE;
    static auto has_next() -> bool;
    template<typename... Args>
    static auto next(typename... Args) -> RETURN_TYPE;
};

Parameters

Name

Description

ID

The method's name.

PARAMETERS

The overrider's parameter list, in parentheses.

...

The overrider's return type.

See Also