unique_ptr with vector resize | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

unique_ptr with vector resize

Hi As we know that vector resize itself on push_back operation when size is reached. This is the reason copy constructor is called when there is resize operation . This behaviour is observed in below code for vector<A> vecInput in attached code. https://code.sololearn.com/ca18A2a25a40/#cpp now, question is why we could not observe either copy or move constructor called with unique_ptr with vector?

14th Feb 2021, 12:37 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 2
You don't observe any move operations because the instances themselves were constructed on the heap, after all. The move constructor of std::unique_ptr<> only has to move the pointer and the deleter it stores, not the entire instance its pointer refers to.
14th Feb 2021, 1:51 PM
Shadow
Shadow - avatar
0
Yup... Got it.... Unique_ptr is storing address , not the object itself ... Thanks a lot
15th Feb 2021, 4:19 AM
Ketan Lalcheta
Ketan Lalcheta - avatar