Type erasure – is that how you call it?
How boost::shared_ptr stores its deleter and how boost::function stores its function object?
Is there any tutorial that teaches the trick?
What is the run-time cost of using type-erased function objects?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The idea is simple, you define a base class that has an interface with the functionality you need, and then inherit from it. Since the type erased class uses only that interface, the actual type underneath is forgotten and erased. Alternatively, if the only needed interface can be expressed as free functions you can store pointers to the free functions.
This is a really simplified smart pointer, but the idea is there. In the particular case of
shared_ptr, the deleter is stored as part of the reference count object, that is held by pointer.