Boost logo

Boost :

Subject: Re: [boost] [type_erasure] Downcasting is now possible
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2015-03-09 20:36:08

On Mon, Mar 9, 2015 at 4:29 PM, Steven Watanabe <watanabesj_at_[hidden]>

> On 03/09/2015 03:04 PM, Matt Calabrese wrote:
> >
> > Awesome. I haven't looked at the implementation and haven't thought about
> > the problem enough, so forgive me if this is a silly question -- why is
> it
> > that register_binding needs to be invoked *manually*? In other words, why
> > can't the instantiation of dynamic_any_cast do something like examine the
> > types of all of the concepts involved, then force an instantiation of a
> > template for each of those concept types, which would perform the
> > registration?
> dynamic_any_cast cannot do this registration because
> it only knows the source and destination concepts.
> It knows the contained type at *runtime* via
> std::type_info, which isn't suitable for template
> instantiations.

Ah, I didn't notice the void(*)() part in the example binding (which is the
target type). I think I understand a bit better now. When I first looked at
the example I though you just needed to register the concepts for some

1) You don't pay for what you don't use. Putting
> the registration in the constructor means that
> it will always happen whether it's needed or not.
> 2) It still won't catch everything. In the example
> I posted, the object is captured by an any type
> which does not support callable, and later cast
> to another any type which does. The constructor
> will therefore not instantiate callable<void()>.
> I think that explicit manual registration is
> less likely to to cause hard to find bugs that
> automatic registration that only works most
> of the time.

Okay, this facility is much different from what I thought it was. I should
have looked at the example code more closely before replying. I read the
description and thought it was for the following type of situation:

void foo() {}
function_any<void()> fun_foo = &foo; // Start by type-erasing via a
callable concept

// Convert to a less-refined concept
plain_any any_foo = fun_foo; // target type is still void(*)(), no
additional indirection

// Get the object back as a function_any<void()>
dynamic_any_cast<function_any<void()>>(any_foo); // should succeed

In other words, I assumed it only worked if the type were originally erased
via the more-refined concept before being converted to the less-refined
any. In this case I imagine you would be able to keep track of the
information by way of the original conversion.

-Matt Calabrese

Boost list run by bdawes at, gregod at, cpdaniel at, john at