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

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;


    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, gregod at, cpdaniel at, john at