Adding any_dynamic_cast?
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.
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
Steven Watanabe wrote:
This idea was proposed a long time ago by Alexander Nasonov. He even used the same name.
I remember Alexander Nasonov's dynamic_any, but my recollection was that it was dynamic in the sense of allowing additional operations on any<> such as for example operator== and so on. Although you're right, it uses this exact technique. http://collaboration.cmc.ec.gc.ca/science/rpn/biblio/ddj/Website/articles/CU... We didn't adopt that for some reason, anybody remember why?
participants (2)
-
Peter Dimov
-
Steven Watanabe