This is one of the places where I get *REALLY* confused - the 'real' code has multiple returns. Apologies: I should have included this psuedo code in the original example. It looks like this.
<snippet> // v is populated somewhere else typedef vector
V; V v; shared_ptr<int> maybe_get_a_pointer() { shared_ptr<int>::iterator itr; itr = find_if(v.begin(), v.end(), some_op);
if(itr != v.end) return(*itr);
shared_ptr<int> p; return(p); } </snippet>
Are compilers supposed to be able to match *itr to p in order to apply
dick.bridges@tais.com wrote: the
NRVO?
I don't know what you mean by "match *itr to p". The NRVO should be possible for the second return statement, because if p is constructed then it's always returned. However, optimisations are by definition optional so the implementation isn't "supposed to" perform them.