S
Staffan Langin
Hello,
I've earlier been told by MS that the upcoming C++ compiler (VS2005) should
utilize NRVO. When compiling the program below with VS2005 Aug CTP release,
it doesn't seem as it performs NRVO. What's the status of VS2005 and NRVO?
Staffan Langin
#include "stdafx.h"
#include <iostream>
struct T
{
T() {std::cout<<"D";}
T(int) {std::cout<<"D";}
T(T const&) {std::cout<<"C";}
T& operator+=(T const&) {return *this;}
};
T
f1()
{
return T();
}
T
f2()
{
T t;
return t;
}
T
f3()
{
T t;
t=T();
return t;
}
T
a1(T const& lhs,T const& rhs)
{
T t(lhs);
t+=rhs;
return t;
}
T
a2(T const& lhs,T const& rhs)
{
T t=lhs;
t+=rhs;
return t;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout<<"min D max D = ";
T t1;
std::cout<<std::endl;
std::cout<<"min D max DC = ";
T t2(T(1));
std::cout<<std::endl;
std::cout<<"min D max DC = ";
T t3=T();
std::cout<<std::endl;
std::cout<<"min D max DCC = ";
T t4(f1());
std::cout<<std::endl;
std::cout<<"min D max DCC = ";
T t5=f1();
std::cout<<std::endl;
std::cout<<"min D max DCC = ";
T t6(f2());
std::cout<<std::endl;
std::cout<<"min D max DCC = ";
T t7=f2();
std::cout<<std::endl;
std::cout<<"min DD max DDCC = ";
T t8(f3());
std::cout<<std::endl;
std::cout<<"min DD max DDCC = ";
T t9=f3();
std::cout<<std::endl;
std::cout<<"min C max CCC = ";
T u1(a1(t1,t2));
std::cout<<std::endl;
std::cout<<"min C max CCC = ";
T u2=a1(t1,t2);
std::cout<<std::endl;
std::cout<<"min C max CCC = ";
T u3(a2(t1,t2));
std::cout<<std::endl;
std::cout<<"min C max CCC = ";
T u4=a2(t1,t2);
std::cout<<std::endl;
return 0;
}
Output:
min D max D = D
min D max DC = D
min D max DC = D
min D max DCC = D
min D max DCC = D
min D max DCC = DC
min D max DCC = DC
min DD max DDCC = DDC
min DD max DDCC = DDC
min C max CCC = CC
min C max CCC = CC
min C max CCC = CC
min C max CCC = CC
I've earlier been told by MS that the upcoming C++ compiler (VS2005) should
utilize NRVO. When compiling the program below with VS2005 Aug CTP release,
it doesn't seem as it performs NRVO. What's the status of VS2005 and NRVO?
Staffan Langin
#include "stdafx.h"
#include <iostream>
struct T
{
T() {std::cout<<"D";}
T(int) {std::cout<<"D";}
T(T const&) {std::cout<<"C";}
T& operator+=(T const&) {return *this;}
};
T
f1()
{
return T();
}
T
f2()
{
T t;
return t;
}
T
f3()
{
T t;
t=T();
return t;
}
T
a1(T const& lhs,T const& rhs)
{
T t(lhs);
t+=rhs;
return t;
}
T
a2(T const& lhs,T const& rhs)
{
T t=lhs;
t+=rhs;
return t;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout<<"min D max D = ";
T t1;
std::cout<<std::endl;
std::cout<<"min D max DC = ";
T t2(T(1));
std::cout<<std::endl;
std::cout<<"min D max DC = ";
T t3=T();
std::cout<<std::endl;
std::cout<<"min D max DCC = ";
T t4(f1());
std::cout<<std::endl;
std::cout<<"min D max DCC = ";
T t5=f1();
std::cout<<std::endl;
std::cout<<"min D max DCC = ";
T t6(f2());
std::cout<<std::endl;
std::cout<<"min D max DCC = ";
T t7=f2();
std::cout<<std::endl;
std::cout<<"min DD max DDCC = ";
T t8(f3());
std::cout<<std::endl;
std::cout<<"min DD max DDCC = ";
T t9=f3();
std::cout<<std::endl;
std::cout<<"min C max CCC = ";
T u1(a1(t1,t2));
std::cout<<std::endl;
std::cout<<"min C max CCC = ";
T u2=a1(t1,t2);
std::cout<<std::endl;
std::cout<<"min C max CCC = ";
T u3(a2(t1,t2));
std::cout<<std::endl;
std::cout<<"min C max CCC = ";
T u4=a2(t1,t2);
std::cout<<std::endl;
return 0;
}
Output:
min D max D = D
min D max DC = D
min D max DC = D
min D max DCC = D
min D max DCC = D
min D max DCC = DC
min D max DCC = DC
min DD max DDCC = DDC
min DD max DDCC = DDC
min C max CCC = CC
min C max CCC = CC
min C max CCC = CC
min C max CCC = CC