List and list elements, where are stored?

前端 未结 7 1644
粉色の甜心
粉色の甜心 2021-01-05 17:59

Given this piece of code:

#include 
(void) someFunction(void) {
    list  l;
    l.push_back(1);
}
  • Where are t
7条回答
  •  粉色の甜心
    2021-01-05 18:44

    The answer you selected is incorrect about 1 thing...

    returning a list does not call the list's copy constructor.

    list func()
    {
      list res; // &res returns 0x7fff183f0900
      res.push_back(10);
      return res;
    }
    
    list l = func();   // &l also returns 0x7fff183f0900 (no copy)
    

    You can double check this by printing &res in the function and &l outside the function.

    The compiler is optimized to pass through returned objects without making copies, otherwise every single non-pointer object returned would be copied, which would be insane.

提交回复
热议问题