Strange C++ behaviour involving multiple calls to destructor
<p>I'm running the following code in Dev Studio 2010:</p>

<pre><code>struct Foo
{
    Foo() {cout &lt;&lt; "Foo()" &lt;&lt; endl;}
    ~Foo() {cout &lt;&lt; "~Foo()" &lt;&lt; endl;}
    void operator ()(const int &amp;) const {}
};

int bar[] = {0};
for_each(begin(bar), end(bar), Foo());
</code></pre>

<p>The output is not what I expected, and is the same in both debug and release regardless of the contents of the "bar" array:</p>

<pre><code>Foo()
~Foo()
~Foo()
~Foo()
</code></pre>

<p>I've looked at the outputted assembly and I can't for the life of me understand why the compiler is generating extra calls to the destructor. Can anyone explain to me exactly what's going on?</p>
