Is using `std::shared_ptr` for in/out variables reasonable?

53 views Asked by At

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:

  1. It allows chaining, i.e. foo = f1(f2(foo)) is valid if f1 and f2 both have the same signature.
  2. Secondly, it allows working with local and temporary variables uniformly, i.e. foo = f1(foo) and another_foo = f1(foo->clone()) both work.

Neither of these two are possible with the original signature.

0

There are 0 answers