return value - C++ - When is the destructor called for an object that is used but not assigned? -


example:

std::unique_ptr<int> getptr() { return std::unique_ptr<int>(new int(5)); } ... void dosomething() {   int x = 2 + *getptr(); } 

when destructor of unique_ptr returned getptr() called within dosomething? called after call 'operator+' or called once leave scope of 'dosomething'?

thanks.

the draft standard n3936 s12.2/3 says:

temporary objects destroyed last step in evaluating full-expression (1.9) (lexically) contains point created.

s1.9/10:

a full-expression expression not subexpression of expression.

s1.9/14:

every value computation , side effect associated full-expression sequenced before every value computation , side effect associated next full-expression evaluated.

note 8:

8) specified in 12.2, after full-expression evaluated, sequence of 0 or more invocations of destructor functions temporary objects takes place, in reverse order of construction of each temporary object.

the full expression rhs of assignment, , sequence point occurs once computation complete. temporary destroyed after '+' computation , before assignment.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -