AMDG On 11/12/2016 06:48 AM, Peter Dimov wrote:
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 can fail if ValueType has virtual bases.
This is pretty cool, and I don't remember seeing this idea anywhere else.
Perhaps we should adopt it?
This idea was proposed a long time ago by Alexander Nasonov. He even used the same name. There was also another (more correct) implementation proposed here: http://lists.boost.org/Archives/boost/2013/11/208957.php
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.
In Christ, Steven Watanabe