I need some advise regarding usage of std::shared_ptr for In/Out parameters. Is having something like
std::shared_ptr<Big> expensive_process(std::shared_ptr<Big> foo)
{
// process foo
...
return foo;
}
considered bad practice (as opposed to the usual way of void expensive_process(Big& foo))?. This signature works uniformly (if adopted everywhere) when foo is either a local variable or a temporary, e.g. expensive_process(another_process(foo)) or expensive_process(foo->clone()).
Rationale
I am currently using the following signature when processing data in-place:
void process(Big& foo);
Changing the signature to shared_ptr<Big> process(shared_ptr<Big> foo) has two main benefits:
- It allows chaining, i.e.
foo = f1(f2(foo))is valid iff1andf2both have the same signature. - Secondly, it allows working with local and temporary variables uniformly, i.e.
foo = f1(foo)andanother_foo = f1(foo->clone())both work.
Neither of these two are possible with the original signature.