przykład kod:Przenoszenie semantyki i podstawowych typów
int main()
{
std::vector<int> v1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::cout << "Printing v1" << std::endl;
print(v1);
std::vector<int> v2(std::make_move_iterator(v1.begin()),
std::make_move_iterator(v1.end()));
std::cout << "Printing v1" << std::endl;
print(v1);
std::cout << "Printing v2" << std::endl;
print(v2);
std::vector<std::string> v3{"some", "stuff", "to",
"put", "in", "the", "strings"};
std::cout << "Printing v3" << std::endl;
print(v3);
std::vector<std::string> v4(std::make_move_iterator(v3.begin()),
std::make_move_iterator(v3.end()));
std::cout << "Printing v3" << std::endl;
print(v3);
std::cout << "Printing v4" << std::endl;
print(v4);
}
wyjściowe:
Printing v1
1 2 3 4 5 6 7 8 9 10
Printing v1
1 2 3 4 5 6 7 8 9 10
Printing v2
1 2 3 4 5 6 7 8 9 10
Printing v3
some stuff to put in the strings
Printing v3
Printing v4
some stuff to put in the strings
pytania
Ponieważ operacje ruch wy n typy pierwotne to tylko kopia, czy mogę założyć, że
v1
pozostanie niezmieniony, czy też stan nie jest określony, nawet w przypadku typów pierwotnych?Zakładam, że powodem, dla którego prymitywne typy nie mają semantyki ruchu, jest to, że kopiowanie jest tak samo szybkie lub nawet szybsze, czy to prawda?
Reguła "ważna, ale nieokreślona" odnosi się tylko do standardowych typów bibliotek ([lib.types.movedfrom]). "Przenoszenie" typów pierwotnych jest kontrolowane przez reguły języka podstawowego, a dla takich typów ruch jest kopią. –