Boost logo

Boost :

Subject: [boost] Adding any_dynamic_cast?
From: Peter Dimov (lists_at_[hidden])
Date: 2016-11-12 08:48:20


I stumbled across

https://github.com/bytemaster/Boost.DynamicAny

which implements, in a fully backward compatible manner, dynamic cast
functionality for boost::any. That is, it can give you X* from an 'any'
holding a value of type Y if Y derives from X.

It achieves this by simply changing

    template<typename ValueType>
    class holder : public placeholder
    {
        ValueType held;

to

    template<typename ValueType>
    class holder : public placeholder, public ValueType

which then enables dynamic_cast from holder<>* to OtherValueType*.

This is pretty cool, and I don't remember seeing this idea anywhere else.

Perhaps we should adopt it?

One might even argue that this is how any_cast should always have worked,
but I suppose we can't change it now for compatibility reasons.

Although it's an interesting question whether C++17 'any' shouldn't be
enhanced in this manner.


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