Bonjour a tous,
J'ai fait quelques tests en mettant des cout dans les constructeurs de copy et constructeurs move d'une classe Obj
J'ai ensuite fait plusieurs push back sur un vector<Obj>...
Et il semble que lors de la réallocation c'est a dire quand le vector "s'agrandit", le vector recopie les données plutôt que de les déplacer... Est-ce normal ?

Voici l'output de mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
first push back 
constructing num1{} rvalue
constructing num1 from '' using move constructor
 
second push back 
constructing num2{} rvalue
constructing num2 from '' using move constructor
constructing num1 from 'num1' using copy constructor
 
third push back 
constructing num3{} rvalue
constructing num3 from '' using move constructor
constructing num2 from 'num2' using copy constructor
constructing num1 from 'num1' using copy constructor
Voici le code du main :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
int main(int argc, const char * argv[])
{
    vector<Obj> vect;
 
    cout << endl << "first push back " << endl;
    vect.push_back(Obj("num1"));
 
    cout << endl << "second push back " << endl;
    vect.push_back(Obj("num2"));
 
 
    cout << endl << "third push back " << endl;
    vect.push_back(Obj("num3"));
 
    return 0;
}
Enfin voici la classe Obj :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
class Obj{
 
    string name;
 
public:
    Obj(const string & nom) : name{nom} {
        cout << "constructing " << name << "{" << nom << "} lvalue" << endl;
    }
    Obj(string&& nom) : name{move(nom)} {
        cout << "constructing " << name << "{" << nom << "} rvalue" << endl;
    }
 
    Obj(const Obj& source) : name{source.name} {
        cout << "constructing " << name << " from '" << source.name << "' using copy constructor" << endl;
    }
    Obj(Obj&& source) : name{move(source.name)}{
        cout << "constructing " << name << " from '" << source.name << "' using move constructor" << endl;
     }
};