shared_ptr<void> is special in that it, by definiton, will invoke undefined behavior by calling delete on a void*.
So, why is there not a shared_ptr<void> specialization which throws a compile error?
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.
shared_ptr<T>is special in that it is by design allowed to hold a pointer to any pointer type which is convertible toT*and will use the proper deleter without UB! This comes into play withshared_ptr<Base> p(new Derived);scenarios, but also includesshared_ptr<void>.For example:
produces the output:
If you visit http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/shared_ptr.htm, scroll down to the assignment section to see the very thing being demonstrated. See http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/sp_techniques.html#pvoid for more details.
EDIT as noted by trinithis, it is UB if the pointer type passed into the constructor is a
void *pointer. Thanks for pointing that out!