In a function like:
template<class Iterator>
A simple_return(Iterator it)
{
return *it;
}
A a = simple_return(my_it);
The compiler can easily perform a RVO, so do that:
template<class Iterator>
A simple_return(Iterator it)
{
A tmp = *it;
return tmp;
}
However, I have seen the second way is sometimes preferable over the former, for example in STL algorithm implementations (gcc), and I want to know if that affects RVO in any way (as std::move(*it)
or std::move(tmp)
does), or has any other reason, for example, regarding conversions or anything else.
For example, reserver_iterator
, instead of:
reference operator*() const
{
return *--Iterator(current);
}
uses:
reference operator*() const
{
Iterator tmp = current;
return *--tmp;
}
I ask that because, for implementing overloadings like operator+
, I use extensively the pattern:
friend A operator+(const A& a, const A& b)
{ return A(a) += b; }
instead of:
friend A operator+(const A& a, const A& b)
{
A tmp(a);
return tmp += b;
}
Which isn't specially more readable but makes it be 3 lines longer (this two sentences in one line would be ugly).