Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Advanced
May 12, 1999

Optimize With the Named Return Value

The named return value is an automatic optimization that a compiler applies in order to eliminate the construction and destruction of a temporary object. When a temporary object is copied to another object using a copy constructor, and none of these objects is const or volatile, the Standard allows the implementation to treat the two objects as one, and not perform a copy at all.
 
  class A {
  public:
    A();
    ~A();
    A(const A&);
    A operator=(const A&);
  };

  A f()  //return A by value
  {
    A a1;
    return a;
  }

  A a2 = f();
The object a1 does not need to be copied when f() returns. Instead, the return value of f() can be constructed directly into the object a2, thereby avoiding both the construction and destruction of a temporary object on the stack.
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap