why NRVO does not kick in in this example?
Hi,
I've noticed strange thing (see code below) -- in NRVO and NRVO2 cases
optimizer does not work (does not elide cctor call). Is there any good
reason for this or it is just lazy compiler? (MSVC 2010, GCC 4.4.3
both behave in the same way)...
#include <cstdio>
#include <exception>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <memory>
using namespace std;
struct A {
int volatile m;
A() : m(0) { cout << "ctor" << endl; }
A(A const& a) : m(a.m) { cout << "cctor" << endl; }
~A() { cout << "dctor" << endl; }
A(int x) : m(x) { cout << "ctor(" << x << ")" << endl; }
A const& operator=(A const& a) { m = a.m; cout << "op=" << endl; }
};
A rvo()
{
return A();
}
A rvo2()
{
switch(rand() % 3)
{
case 0: return A(0);
case 1: return A(1);
case 2: return A(2);
}
if (rand() & 1) return A(10);
return A(20);
}
A nrvo()
{
if (rand() > 1)
{
A a1(rand() % 3);
return a1;
}
return A(20);
}
A nrvo2()
{
A a1(rand() % 3);
if (rand() > 1)
{
return a1;
}
return a1;
}
int main()
{
srand(time(0));
cout << "----- RVO" << endl;
{ A a = rvo(); }
cout << "----- RVO2" << endl;
{ A a = rvo2(); }
cout << "----- NRVO" << endl;
{ A a = nrvo(); }
cout << "----- NRVO2" << endl;
{ A a = nrvo2(); }
cout << "----- new A(rvo())" << endl;
{ auto_ptr<A> a(new A(rvo())); }
cout << "----- A(rvo())" << endl;
{ A(rvo()); }
cout << "----- A a(rvo())" << endl;
{ A a(rvo()); }
return 0;
}
Also, quite surprisingly A(rvo()) case gets completely optimized away
(I would expect volatile to prevent that... probably compiler decided
that no one else has access to that variable).
Sincerely yours, Michael.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]