From 31f716589b5301136275ec417215b2098c451f33 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Tue, 14 Jan 2020 19:10:48 -0500 Subject: [PATCH] Better explanation for std::move. --- CPP11.md | 4 ++-- README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CPP11.md b/CPP11.md index b9bfd8c..feca461 100644 --- a/CPP11.md +++ b/CPP11.md @@ -648,7 +648,7 @@ In C++14, `decltype(auto)` can be used instead. ## C++11 Library Features ### std::move -`std::move` indicates that the object passed to it may be moved, or in other words, moved from one object to another without a copy. The object passed in should not be used after the move in certain situations. +`std::move` indicates that the object passed to it may have its resources transferred. Moves can often be more efficient than copies. Using objects that have been moved from should be used with care, as they can be left in an unspecified state (see: [What can I do with a moved-from object?](http://stackoverflow.com/questions/7027523/what-can-i-do-with-a-moved-from-object)). A definition of `std::move` (performing a move is nothing more than casting to an rvalue): ```c++ @@ -660,7 +660,7 @@ typename remove_reference::type&& move(T&& arg) { Transferring `std::unique_ptr`s: ```c++ -std::unique_ptr p1 {new int{0}}; +std::unique_ptr p1 {new int{0}}; // in practice, use std::make_unique std::unique_ptr p2 = p1; // error -- cannot copy unique pointers std::unique_ptr p3 = std::move(p1); // move `p1` into `p3` // now unsafe to dereference object held by `p1` diff --git a/README.md b/README.md index c82108a..e986652 100644 --- a/README.md +++ b/README.md @@ -1460,7 +1460,7 @@ In C++14, [decltype(auto)](#decltypeauto) can be used instead. ## C++11 Library Features ### std::move -`std::move` indicates that the object passed to it may be moved, or in other words, moved from one object to another without a copy. The object passed in should not be used after the move in certain situations. +`std::move` indicates that the object passed to it may have its resources transferred. Moves can often be more efficient than copies. Using objects that have been moved from should be used with care, as they can be left in an unspecified state (see: [What can I do with a moved-from object?](http://stackoverflow.com/questions/7027523/what-can-i-do-with-a-moved-from-object)). A definition of `std::move` (performing a move is nothing more than casting to an rvalue): ```c++ @@ -1472,7 +1472,7 @@ typename remove_reference::type&& move(T&& arg) { Transferring `std::unique_ptr`s: ```c++ -std::unique_ptr p1 {new int{0}}; +std::unique_ptr p1 {new int{0}}; // in practice, use std::make_unique std::unique_ptr p2 = p1; // error -- cannot copy unique pointers std::unique_ptr p3 = std::move(p1); // move `p1` into `p3` // now unsafe to dereference object held by `p1`