I just have a dilemma, how should I return smart pointer from a function, when function might fail. I could pick one of the following options:
- Return pointer, and throw exception if function fails:
std::shared_ptr foo() { // ... if (!ok) throw; return ptr; }
- Return pointer, and return empty pointer, if function fails
std::shared_ptr foo() { // ... if (!ok) return std::shared_ptr(); return ptr; }
- Pass pointer by reference, and return bool flag
bool foo(std::shared_ptr& ptr) { // ... if (ok) ptr = ...; return ok; }
Is there any best practice, guideline, how to report, that function didn't execute properly? Or is it usually project-specific?
Thanks for your answers