
15 Jul
2008
15 Jul
'08
2:17 p.m.
What is so bad in general about returning uninitialized smart pointers? Is that more costly than throwing an exception? I mean, there is even some support for testing a smart pointer for validity built into shared_ptr, so why not use it where appropriate?
The only problem with returning a null pointer, or -1, or whatever in case of error is that it's easy to ignore at the calling scope, so the upper layers would not even know the error occurred. Exception, on the other hand, propagates up the stack enforcing more structured error handling.
I agree, but only if the case is an error. In my application, I have several places where the "null" value is a regular result. That's what I meant by saying "where appropriate". Cheers, Filip