Passing the string `(* sp) [i]` of the matrix `shared_ptr <vector <vector <T>> sp` to a function that accepts` shared_ptr <vector <T>> `

I have std::shared_ptr<std::vector<std::vector<double>> sp

and need to pass (*sp)[i]

for some i

to a function foo

that takes std::shared_ptr<std::vector<double>>

. How can i do this?

One option would be foo(std::make_shared<std::vector<double>>((*sp)[i])

, but this will create a copy of the vector (*sp)[i]

. Since it (*sp)[i]

can be huge, I absolutely don't want this to happen.

So, is it possible to transfer (*sp)[i]

to foo

without creating a copy?

EDIT . Perhaps we could do the following:

foo(std::shared_ptr<std::vector<double>>(&(*sp)[i], [](std::vector<double>* p) { }));

      

+3


source to share


2 answers


Use the alias constructor.



foo(std::shared_ptr<std::vector<double>>(sp, &(*sp)[i]));

      

+5


source


It looks like your best options would be either

A) Modify foo to accept a reference to shared_ptr, which will eliminate passing by value.

or



B) Use std :: move to move the shared_ptr to foo and then make sure you don't reference sp after the call.

As Quentin said, otherwise it is not possible with your current setup.

0


source







All Articles