0
#include <iostream>

using namespace std;

void swap(int& a, int& b) 
{

    cout << "address of a: " << &a << " value of a: " << a << endl;
    cout << "address of b: " << &b << " value of b: " << b << endl;

    int tmp{move(a)};

    cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;

    a = move(b);
    b = move(tmp);

    cout << "address of a: " << &a << " value of a: " << a << endl;
    cout << "address of b: " << &b << " value of b: " << b << endl;
    
}

void swap_no_move(int& a, int& b)
{

    cout << "address of a: " << &a << " value of a: " << a << endl;
    cout << "address of b: " << &b << " value of b: " << b << endl;

    int tmp{ a };

    cout << "address of tmp: " << &tmp << " value of tmp: " << tmp << endl;

    a = b;
    b = tmp;

    cout << "address of a: " << &a << " value of a: " << a << endl;
    cout << "address of b: " << &b << " value of b: " << b << endl;

}

int main() {
    
    int a = 10;
    int b = 5;

    swap(a, b);

    cout << endl;

    int c = 10;
    int d = 5;

    swap_no_move(c, d);

    cin.get();

    return 0;
}

enter image description here

I have two swap functions: swap and swap_no_move. According to what I read from the book, there should be no "copy" in function swap which means the address of tmp should be the same for tmp and an in function swap. However, the output I got shows there is no difference between these two functions, did I do something wrong?

2
  • 1
    Please don't post images of text. Please edit your question and copy-paste the output as text. Commented Oct 20, 2020 at 6:23
  • 1
    I'm not entirely sure what you've expected to happen. std::move doesn't actually move anything. In fact it's only a cast. For primitive values there will always copying involved.
    – Lukas-T
    Commented Oct 20, 2020 at 6:25

1 Answer 1

2

The definition

int tmp{move(a)};

doesn't move the reference or the variable a itself. It creates a brand new variable tmp which the compiler allocates space for. Then the value of a is moved into tmp.

And since moving int values can't really be done, it's exactly the same as

int tmp = a;
3
  • 1
    "And since moving int values can't really be done" might be clearer as "since int doesn't contain any pointers". This isn't entirely accurate, but it helps new users understand the concept. Commented Oct 20, 2020 at 16:28
  • Thank you for your reply, but I am still confused about right value reference. From my understanding through right reference the "copy" action can be skipped which means there is no need to allocate space in memory, is it possible? If so, would you like to give me some examples?
    – will_xdxd
    Commented Oct 21, 2020 at 7:54
  • @will_xdxd If you define a variable that is not a reference (like tmp in both your functions), then it's a distinct variable which needs separate space allocated for it. There's no way to initialize it so it shares memory with any other variable. Commented Oct 21, 2020 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.