Suppose I have a class called A, and a void pointer vp. Can the following ever cause an exception?
A *ap = reinterpret_cast<A*>(vp);
Thank you, Fijoy
No, neither a reinterpret_cast<T>
nor its C-style cast equivalent perform any checking, so they cannot by themselves cause an exception. Obviously, since both constructs are about as unsafe as it gets, dereferencing the result pointer ap
could cause undefined behavior.
Assuming (which you can in your case since it's of type void*
) the expression vp
doesn't throw an exception (it could do if it was an object of a type that had a hand-crafted conversion operator that threw an exception), then
A *ap = reinterpret_cast<A*>(vp);
will not itself throw an exception.
dereferencing ap
could cause an exception to be thrown however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With