vendredi 23 décembre 2016

Using std::move with vectors

I have a question about using std::move in c++.

Let's say that I have following class, which in it's constructor takes vector as parameter:

class A
{
public:
    A(std::vector<char> v): v(v) {}
private:
    std::vector<char> v;
};

But if I write somewhere:

std::vector<char> v;
A a(v);

The copy constructor of vector will be called twice, right? So should I write A constructor like this?

class A
{
public:
    A(std::vector<char> v): v(std::move(v)) {}
private:
    std::vector<char> v;
};

And what if I would like to call:

std::vector<char> v;
A a(std::move(v));

Is that okay with second version of constructor or should maybe create another A constructor which takes std::vector&& ?

Aucun commentaire:

Enregistrer un commentaire