Subject: Re: [boost] [type_erasure] Downcasting is now possible
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2015-03-09 19:29:23
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
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. If it were able to do such registration,
then no registration would be necessary in the first
place, since that would mean that dynamic_any_cast
knows everything it needs statically.
The only place where such an instantiation could
happen is in the constructor of any, since the
constructor is the boundary where the type is erased.
I chose not to do this for two reasons:
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.
> To be clear, the purpose of this instantiation would be to
> create a type that has a static member with a constructor that performs the
> registration. You'd just need to then "touch" the object to make sure the
> constructor actually gets run. Since the registration is done via an
> instantiation of a template that uses the concept as an argument, that
> registration is guaranteed to only happen exactly once per concept,
> regardless of how many times the dynamic_any_cast is used across
> translation units.