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
7 ответов
+ 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
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.
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
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