0

move constructor

I understand move constructor need , but when it is useful is confusing me at the moment. Why it is helpful is not concern , but example when it is useful is bothering me. 1. Assume I have a method where object is constructed and returned, then here Named Return Value Optimization (NRVO) or Return Value Optimization (RVO) done during optimization helps and copy is not created. Is this the copy elision or something different? 2. If object is created and passed as an argument to another function, pass by values saves our unnecessary copy. In which case, move constructor helps?

29th Jan 2025, 6:55 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Answers
+ 1
MyClass createObject() { MyClass obj; return obj; // With RVO or NRVO, no copy/move is made here. }
30th Jan 2025, 11:19 AM
Bharat Bhushan
Bharat Bhushan - avatar
+ 1
Ketan Lalcheta RVO is one type of copy elision. However copy elision is a compiler optimization which is not guaranteed in all possible cases, depending on compiler design and configuration. Which is why a move/copy constructor is still required for the Class. Otherwise, the program may not work correctly if the copy elision does not occur.
30th Jan 2025, 12:09 PM
Shardis Wolfe
0
Yes , this nrvo is constructing object at the place where createObject method is called eliminating need of move or copy But when move is needed ?
30th Jan 2025, 11:48 AM
Ketan Lalcheta
Ketan Lalcheta - avatar