Boost logo

Boost :

From: Jean-Louis Leroy (jl_at_[hidden])
Date: 2025-05-25 21:09:43


> As I often use GMock I would prefer same pattern here,
> return type, name, args
> this is example from GMock docs(ignore override):
> MOCK_METHOD(void, Forward, (int distance), (override));

Same as YOMM2:

    declare_method(void, poke, (virtual_<Animal&>, std::ostream&));

    define_method(void, poke, (Dog& dog, std::ostream& os)) {
        os << "bark";
    }

So why did I change it to this?

    BOOST_OPENMETHOD(poke, (virtual_ptr<Animal>, std::ostream&), void);

    BOOST_OPENMETHOD_OVERRIDE(
        poke, (virtual_ptr<Dog> dog, std::ostream& os), void) {
        os << "bark";
    }

That's because the YOMM2 syntax does not work well with return types that
contain commas, like `std::tuple<int, float>`, forcing you to use tricks like
typedefs or `BOOST_IDENTITY_TYPE`. The new syntax requires that the method name
be an identifier, so we know for sure that the first macro argument is the
entire thing, not a bit of it (e.g., not `std::tuple<int`). The second is
wrapped in parentheses. That always gives a single argument. The return type and
optional registry (if not using the default) are simply `__VA_ARGS__`. Maybe
the return type is split into several macro arguments, but it doesn't matter,
because it is passed as a whole to the `method` template.

> I am not a fan of:
> poke, void(virtual_ptr<Animal>)
> since afaik there is no C++ syntax that nas name, return type, arguments
> ordering.

It is a valid function type, as in `std::function<int(char, float)>`. To sum up:

    void(virtual_ptr<Animal>) : valid function type
    auto(virtual_ptr<Animal>) -> void : same valid function type
    (virtual_ptr<Animal>) -> void : NOT valid

J-L


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk