I do not claim they will optimize temporaries. I claim they can optimize without seeing the function.
But for the best practice bit, I simply disagree. Habits can cause problems, but they are also individual.
Btw, for that matter, for this code:
Code:
#include <iostream>
class Iterator
{
private:
int m_i;
public:
Iterator(): m_i(0) {}
Iterator& operator ++ () { m_i++; return *this; }
Iterator operator ++ (int) { Iterator temp(*this); m_i++; return temp; }
bool operator < (int i) { return m_i < i; }
int operator * () { return m_i; }
};
int main()
{
for (Iterator it; it < 10; it++)
std::cout << *it;
for (Iterator it; it < 10; ++it)
std::cout << *it;
}
I get this assembly output with optimizations (even better with inlining):
00D21010 inc dword ptr [eax]
00D21012 ret
00D21020 mov ecx,dword ptr [edx]
00D21022 mov dword ptr [eax],ecx
00D21024 inc ecx
00D21025 mov dword ptr [edx],ecx
00D21027 ret
Hardly expensive.