Boost logo

Boost :

From: Jean-Louis Leroy (jl_at_[hidden])
Date: 2025-03-11 12:50:33


> Doesn't show the overloading part, but that's OK.

> Quick question: Given the ability to return different (necessarily related?)
> types from the same open-method, can we say BOM allows co-variant return type?
> Like virtual methods / polymorphism allows, just more flexible that plain
> pointers?

Yes. And in that case, the return type can act as a tie-breaker, as described in
N2216.

Here is `transpose` with `const` and covariant return types:

    BOOST_OPENMETHOD(
        transpose, (shared_virtual_ptr<const Matrix>),
        shared_virtual_ptr<const Matrix>);

    BOOST_OPENMETHOD_OVERRIDE(
        transpose, (shared_virtual_ptr<const OrdinaryMatrix>),
        shared_virtual_ptr<const OrdinaryMatrix>) {
        return make_shared_virtual<OrdinaryMatrix>();
    }

    BOOST_OPENMETHOD_OVERRIDE(
        transpose, (shared_virtual_ptr<const SymmetricMatrix> m),
        shared_virtual_ptr<const SymmetricMatrix>) {
        return m;
    }

You can call a specific overrider like so:

    auto u = BOOST_OPENMETHOD_OVERRIDERS(
        transpose)<shared_virtual_ptr<const SymmetricMatrix>(
        shared_virtual_ptr<const SymmetricMatrix>)>::fn(m);
    static_assert(
        std::is_same_v<decltype(u), shared_virtual_ptr<const SymmetricMatrix>>);

(Compiler Explorer: https://godbolt.org/z/ndKaKWPzd)

J-L

On Tue, Mar 11, 2025 at 4:59 AM Dominique Devienne <ddevienne_at_[hidden]> wrote:
>
> On Mon, Mar 10, 2025 at 9:45 PM Jean-Louis Leroy via Boost
> <boost_at_[hidden]> wrote:
> > > > [...] handling of const-ness
> > > `const` is supported.
>
> Doesn't show the overloading part, but that's OK.
>
> Quick question: Given the ability to return different (necessarily
> related?) types
> from the same open-method, can we say BOM allows co-variant return type?
> Like virtual methods / polymorphism allows, just more flexible that
> plain pointers?
>
> Thanks, --DD


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