dimanche 5 juillet 2020

Vector of structs with unique_ptr

I've already read many answers for the similar questions, but still cannot find a solution for my problem.

I have the struct:

struct Param {
    std::string name;
    std::unique_ptr<Object> default_val;
};

I have the class Function that stores a vector of Params. Somewhere I create a vector of this params and need to pass it to Function constructor.

The problem is in the moment when I'm trying to pass vector to constructor. I know that I need to move all unique_ptrs, and I do it.

Maybe I miss something pretty simple?

Code snippets

The Function:

using Params = std::vector<Param>;

class Callable : public Object {
public:
    Callable(const Params & params) : params(params) {}
// ...
private:
    Params params;
}

The code where I create vector:

Params params;
for(auto & p : func_decl->params){
    auto default_val = eval(p.default_val.get());
    Param p(p.id->get_name(), std::move(default_val));
    params.push_back(std::move(p));
}

Is it possible not to use here shared_ptr?

The output is: error: use of deleted function 'Param::Param(const Param&)'

Aucun commentaire:

Enregistrer un commentaire