Boost logo

Boost :

Subject: Re: [boost] Adding any_dynamic_cast?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2016-11-12 09:43:19


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


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