struct String
{
  String()
  {
    std::cout<<"String::String(), this="<<this<<std::endl;
  }

  ~String()
  {
    std::cout<<"String::~String(), this="<<this<<std::endl;
  }

  String(String const &other)
  {
    std::cout<<"String::String(String const &other), this="<<this;
    std::cout<<", other="<<&other<<std::endl;
  }

  String& operator=(String const &other)
  {
    std::cout<<"String::operator=(String const &other), this="<<this;
    std::cout<<", other="<<&other<<std::endl;
    return *this;
  }	
};

String const operator*(String const &lhs, String const &rhs)
{
  String temp(rhs);
  std::cout<<"return by value(* operator)"<<std::endl;
  return temp; //without copy construct
}

int main()
{													
  std::cout<<"***************value******************"<<std::endl;
  String D;
  String E;
  String F;
  D = E * F;
  std::cout<<"***************value******************"<<std::endl<<std::endl;	
		
	
  std::cin.get();

  return 0;
}

Compiler vc2010
OS win7 64bits

I expected it would copy construct temp before calling copy assignment
What kind of optimization did it done?NRVO?
If NRVO did it, what is the limit of NRVO?
Thanks

Recommended Answers

That is obviously a return value optimization, I mean, it's a textbook example of NRVO. That's what you would expect any half-decent compiler to do.

I don't understand your question about the "limit" of NRVO. If you mean, how complex a situation can the compiler cope with, then I …

Jump to Post

All 4 Replies

That is obviously a return value optimization, I mean, it's a textbook example of NRVO. That's what you would expect any half-decent compiler to do.

I don't understand your question about the "limit" of NRVO. If you mean, how complex a situation can the compiler cope with, then I would guess that is pretty compiler-dependent.

how complex a situation can the compiler cope with, then I would guess that is pretty compiler-dependent.

That is what I mean, it is a good news that we have && now.

I assume you are referring to rvalue-references, not the logic "and" operator.

I assume you are referring to rvalue-references, not the logic "and" operator.

of course, next time I will say rvalue-references rather than &&

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.