0

Why not move constructor

I was expecting move constructor . Why not? https://sololearn.com/compiler-playground/cAbUWJKaaQa2/?ref=app

30th Jan 2025, 4:06 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
13 Réponses
+ 2
Ketan Lalcheta triggering a move occurs if the r-value already exists somewhere. For the call display(test()) in your main(), The compiler needs to map a test class r-value to the obj argument l-value in display(). Since no test class object exists at that point, test() triggers the constructor.
31st Jan 2025, 11:34 AM
Shardis Wolfe
+ 1
Ketan Lalcheta Your code does not try to move or copy your test class object to any other variable. Your display() function calls the constructor as part of resolving the function argument and assigns it to the obj variable inside the display() method. You don't do anything else with the object at that point, so the destructor is called when the display() method ends and the scope closes.
30th Jan 2025, 4:36 PM
Shardis Wolfe
+ 1
maybe specifically use move? int main() { test t; display(test(move(t))); return 0; }
31st Jan 2025, 1:40 AM
Bob_Li
Bob_Li - avatar
+ 1
But test is temporary object meaning it is already the r value Bob_Li
31st Jan 2025, 3:56 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
returning from a function can also trigger the move semantics. move under the hood is just metadata manipulations. It can't work in of itself. you must also have to have some other object to move the r value reference to. also, eagerly imposing move everywhere is not a really good idea, specially if your data ends up being shared in different parts of your code. C++ is less strict with move than Rust, so you have use-after-move bugs with C++...
31st Jan 2025, 6:43 AM
Bob_Li
Bob_Li - avatar
+ 1
Ketan Lalcheta void display(test&& obj) is used for rvalue reference arguments. void display(const test& obj) is used for lvalue reference arguments. test() in your display(test()) is an rvalue. When passing rvalue reference arguments, the rvalue is assigned directly to the function argument. So no move or copy takes place. If you instead had, test t = test(); display(t); That would trigger the lvalue reference argument version of display(). In which case a move/copy would likely occur.
1st Feb 2025, 5:25 AM
Shardis Wolfe
0
Shardis Wolfe I don't want to perform any operation on object , so destructor is okay for me. As test() is not named value and hence not a lvalue, it is a r value . So, it should not call constructor , but should map to move constructor. Isn't my understanding correct?
31st Jan 2025, 3:58 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
just because it's an r value is not enough reason for it to be moved. as it is in your code, it's just a function argument. in display(test()) you're not using test() as constructor for another test.
31st Jan 2025, 5:30 AM
Bob_Li
Bob_Li - avatar
0
Do you mean to say r value does not call move and move is only called if it was made r value forcefully by making use of std::move?
31st Jan 2025, 6:04 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Shardis Wolfe agreed that object does not exists so it has to trigger a constructor. Now it can be copied or moved to be passed to a function argument. Why it is not moving as function taking && is called as per output of the code?
31st Jan 2025, 12:08 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I do not understand anything in that code
31st Jan 2025, 8:14 PM
Astro link
Astro link - avatar
0
Just curious when will move gets called. Any example will be helpful Shardis Wolfe . Bob_Li Even explicitly using move also don't call move as below: display(move(test()))
1st Feb 2025, 3:14 PM
Ketan Lalcheta
Ketan Lalcheta - avatar