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
Rodrigo Corral [MVP] - 06 Sep 2005 08:42 GMT
http://msdn.microsoft.com/chats/transcripts/vstudio/vstudio_022703.aspx
Here you can read:
Host: Jeff (Microsoft)
Q: I am surprised to hear that Everett does not implement RVO. Does it
implement NRVO (named-RVO) at all?
A: We do RVO, just not NRVO.

Signature
Un saludo
Rodrigo Corral González [MVP]
FAQ de microsoft.public.es.vc++
http://rcorral.mvps.org
Staffan Langin - 06 Sep 2005 09:40 GMT
> http://msdn.microsoft.com/chats/transcripts/vstudio/vstudio_022703.aspx
>
[quoted text clipped - 4 lines]
>
> A: We do RVO, just not NRVO.
Rodrigo,
That transcript seems to refer to VS2003 (Everett) and not VS2005. If one
looks at the output from the program in my original post, VS2005 Aug CTP
release also seems to support RVO but not NRVO but I'd like someone from MS
to confirm that's the specification of the final version aswell.
Staffan
Tom Widmer [VC++ MVP] - 19 Sep 2005 12:20 GMT
> 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?
I heard that it was implemented, but only for release builds. Did you
compile with optimization enabled? See:
http://groups.google.co.uk/group/microsoft.public.vc.language/msg/36a9d8129bcfba
c6?dmode=source&hl=en
Tom
Staffan Langin - 25 Sep 2005 13:42 GMT
> I heard that it was implemented, but only for release builds. Did you
> compile with optimization enabled? See:
>
> http://groups.google.co.uk/group/microsoft.public.vc.language/msg/36a9d8129bcfba
c6?dmode=source&hl=en
Tom,
Thanks a lot for you reply. It indeed seems as NRVO is enabled when
compiling a release build. This is very good news for me as VS2005 now seems
to be viable option for my project. Again, thanks for your reply.
Staffan Langin