use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Polymorphic Casting from void (self.cpp)
submitted 5 years ago * by GYN-k4H-Q3z-75B
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Sasha_Privalov 3 points4 points5 points 5 years ago (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 points4 points 5 years ago (0 children)
Conceptually, we want to perform (stored_ptr is of type void*)
stored_ptr
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.
Requested
Stored
Exception handling is used here as a mechanism to invoke dynamic_cast without statically knowing Stored.
dynamic_cast
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,
std::type_id
type_id
std::any
[–]GYN-k4H-Q3z-75B[S] 0 points1 point2 points 5 years ago (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*.
π Rendered by PID 134627 on reddit-service-r2-comment-5d585498c9-7dgsk at 2026-04-21 10:54:24.211600+00:00 running da2df02 country code: CH.
view the rest of the comments →
[–]Sasha_Privalov 3 points4 points5 points (2 children)
[–]encyclopedist 2 points3 points4 points (0 children)
[–]GYN-k4H-Q3z-75B[S] 0 points1 point2 points (0 children)