-2

Is there a language syntax for move rather than std::move()? And how does std::move() work?

In the rule of five I have to define move constructor.

Code using std::move()

#include <cstdio>
#include <vector>
#include <string>
#include <utility>
using namespace std;
int main() {
    vector<string> v1 = { "one", "two", "three", "four", "five" };
    vector<string> v2 = { "six", "seven", "eight", "nine", "ten" };
    
    auto v3 = std::move(v2);
    
    return 0;
}
Ibrahim zawra
  • 317
  • 1
  • 2
  • 11
  • "_is there a language syntax for move rather the `std::move`_" - No, [`std::move`](https://en.cppreference.com/w/cpp/utility/move) doesn't actually move anything. Regarding best practices: [The rule of three/five/zero](https://en.cppreference.com/w/cpp/language/rule_of_three) may help. – Ted Lyngmo Mar 09 '21 at 22:08
  • Okay, I will add it, thanks for the link. – Ibrahim zawra Mar 09 '21 at 22:15
  • why the question is closed?, saying seeking recommendations for books, software libraries???!!! – Ibrahim zawra Mar 10 '21 at 20:12
  • It's probably the side-question "_what is the best practice_" which may be perceived as calling for opinions (although that's not the reason given for closing the question). Perhaps you'll get more votes to reopen the question if you just remove or rewrite that part. – Ted Lyngmo Mar 10 '21 at 20:23
  • I want to benefit from the experiences of the experts here, and I think I'm here for that!!!, I don't understand what is the wrong with that? – Ibrahim zawra Mar 10 '21 at 20:37
  • 1
    I get that, but the site rules are that questions asking for opinions are off topic. There are however humans deciding when a question isn't following the rules. Sometimes it's a close call. I personally don't think this question should have been closed for that reason - and I do not understand the reason that was actually given either (which is why I voted to reopen it). – Ted Lyngmo Mar 10 '21 at 20:49

1 Answers1

2

is there a language syntax for move rather the std::move

No. The only language syntax related to moves is rvalue references, which is what gives code the permission to move data from one object to another rather than copying the data, since the target of an rvalue reference is not expected to care about the data anymore after a move has been performed.

and how std::move works?

std::move() merely type-casts an lvalue reference into an rvalue reference, nothing more. It is the responsibility of the code that is receiving the rvalue reference to actually move data around as needed. Use of std::move() does not guarantee that a move will actually be performed. It is merely providing permission to perform a move on an object that is not normally an rvalue.

In your example, std::move(v2) casts v2 to a vector<string>&& rvalue reference, and assigns that to v3. No actual move is being performed, since v3 is just a reference that is not being used for anything. A more meaningful example would be:

vector<string> v1 = { "one", "two", "three", "four", "five" };
vector<string> v2(std::move(v1));
vector<string> v3;
v3 = std::move(v2);

std::vector has a move constructor and a move assignment operator. In this case, v2 will steal the array from v1 leaving v1 empty, and then v3 will steal the array from v2leavingv2empty. There is only 1 physical array being allocated in memory, pointers to it are simply being moved around from onevector` to another.

In the rule of five I have to define move constructor, what is the best practices?

Move what you need, and leave the source object in a "stable but unspecified" state. Which is just a fancy way of saying: steal away the source object's data, and make sure the source object is not left in a state where it will crash or otherwise have unspecified behavior because of the steal. Which, in most typical cases just means setting stolen pointers/handles to nullptr/0.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks Remy, do you mean by "No actual move is being performed" that only the reference had been passed to v3? – Ibrahim zawra Mar 09 '21 at 22:47
  • " most typical cases just means setting stolen pointers/handles to nullptr/0", what if i'm dealing with a complicated object's data. – Ibrahim zawra Mar 09 '21 at 22:50
  • Thanks Remy. That is helpful. – Ibrahim zawra Mar 09 '21 at 23:25
  • 2
    @RemyLebeau `auto` doesn't deduce reference types like that, it uses the same rules as template type deduction and so won't deduce to a reference type (rvalue or otherwise) unless you write e.g. `auto&&` (or `decltype(auto)`). `auto` by itself will deduce to `std::vector` in this case. – Kyle Mar 10 '21 at 17:38
  • @Kyle "*`auto` ... won't deduce to a reference type*" - oh right, I keep forgetting about that. I don't use `auto` often. – Remy Lebeau Mar 10 '21 at 17:51