move семантика при релокации и слиянии вектора
int main()
{
std::cout << "v1 creation" << std::endl;
std::vector<Movable> v1{
std::string{"1"},
std::string{"2"},
std::string{"3"},
std::string{"4"},
std::string{"5"} };
std::cout << "v1 creation end" << std::endl;
std::cout << "v2 creation" << std::endl;
std::vector<Movable> v2{
std::string{"6"},
std::string{"7"},
std::string{"8"},
std::string{"9"},
std::string{"10"} };
std::cout << "v2 creation end" << std::endl;
v1.reserve(v1.size() + v2.size());
std::cout << "move" << std::endl;
v1.insert(v1.end(), std::make_move_iterator(v2.begin()), std::make_move_iterator(v2.end()));
std::cout << "v1: " << v1.size() << std::endl;
for (const auto& it : v1)
{
it.print();
}
std::cout << "v2: " << v2.size() << std::endl;
for (const auto& it : v2)
{
it.print();
}
return 0;
}
Вывод:
...
v2 creation end
Ctor <const Movable&>
Ctor <const Movable&>
Ctor <const Movable&>
Ctor <const Movable&>
Ctor <const Movable&>
move
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
v1: 10
1
2
3
4
5
6
7
8
9
10
v2: 5
Если закоментировать строку v1.reserve(v1.size() + v2.size());, то вывод меняется на
v2 creation end
move
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
Ctor <Movable&&>
...
Вопрос1: почему при релокации вектора, вызываемой reserve вызывается конструктор копирования, а не мув-конструктор и можно ли от вектора добиться вызова мув-конструкторов при релокации?
Вопрос2: почему при вставке без reserve мы уже видим, что импользуется мув-семантика?