jeudi 1 septembre 2016

Copying vector

I have a:

std::vector<std::shared_ptr<T>>

Which I would like to copy to a

std::vector<std::shared_ptr<const T>>

Now I noticed that if I do this:

class A
{
public:
    A(const std::vector<std::shared_ptr<int>>& list) : internalList(list.begin(), list.end()) {}
    std::vector<std::shared_ptr<const int>> internalList;
};

it compiles fine (clang++ std==c++14) but if I do:

class A
{
public:
    A(const std::vector<std::shared_ptr<int>>& list) : internalList(list) {}
    std::vector<std::shared_ptr<const int>> internalList;
};

I find it strange that when I use a copy constructor it doesn't work because it can't figure out the conversion from non-const to const?

xxxx.cpp:672:56: error: no matching constructor for initialization of 'std::vector<std::shared_ptr<const int> >'

Could someone explain why please, and if the way I do it (using iterator in the constructor) is the best solution?

Aucun commentaire:

Enregistrer un commentaire