c++ - Can compiler optimize return value from copy to a reference? -
if write code way:
shared_ptr<foo> bar::getfoo() { return m_foo; } void somewhereelse() { shared_ptr<foo> foo = mybar.getfoo(); //do stuff foo }
can compiler somehow optimize code changed that:
void somewhereelse() { const shared_ptr<foo> &foo = mybar.getfoo__by_ref_somehow(); //do stuff foo }
i'm asking because problem in case of concurrent execution.
i understand rvo can applied , remove copy, compiler eliminate copy @ all?
no. asked value, get value. copy elision eliding unnecessary copies. wanted value; function returns value. you're going @ least 1 copy. you're wanting form of code transformation, turning value reference.
you create function getfoo__by_ref_somehow
, returns const&
value stored in class. different function create, not compiler allowed create you.
Comments
Post a Comment