0
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

Edited by stereomatching: n/a

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by stereomatching
0

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.

0

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.

0

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.