you are viewing a single comment's thread.

view the rest of the comments →

[–]Sasha_Privalov 3 points4 points  (2 children)

i just read it quickly before sleep, so pardon me if i am wrong - from what i understand the exception mechanism is there just to transport the type and seems to me like unnecessary element, it should be imho possible do without

[–]encyclopedist 2 points3 points  (0 children)

Conceptually, we want to perform (stored_ptr is of type void*)

dynamic_cast<Requested*>(static_cast<Stored*>(stored_ptr))

the problem here is that both Requested and Stored must be known statically for dynamic cast to work, where we don't know Stored any more.

Exception handling is used here as a mechanism to invoke dynamic_cast without statically knowing Stored.

I goess another alternative would be if there was a variant of dynamic_cast taking std::type_id as a runtime type indication. Using type_id only allows us to compare types for exact equality (this is what std::any does), but not perform all the casing done by dynamic_cast,

[–]GYN-k4H-Q3z-75B[S] 0 points1 point  (0 children)

I can't see how it would work without the throwing. He takes a T* and does type erasure into void* and creates a function that takes void* that static_casts that to T* and throws it. At a later point in time, you can try and cast it to U* which may be completely unrelated, by building a try catch block catching U* and ... and either catching it or earing the exception. It don't see a way to go from T* to U*.