Home arrow C++ arrow Page 2 - Temporary Variables: Chasing Temporaries Away
C++

Temporary Variables: Chasing Temporaries Away


There are situations in C++ when it is good to return the result of a function by value rather than by reference. There is usually a price to be paid, however...unless the compiler can be made to help. Jun Nakamura explains.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
October 10, 2005
TABLE OF CONTENTS:
  1. · Temporary Variables: Chasing Temporaries Away
  2. · Return Value Optimization
  3. · Vector3 test
  4. · Details, details… what’s that… small print?
  5. · Help the compiler help you

print this article
SEARCH DEVARTICLES

Temporary Variables: Chasing Temporaries Away - Return Value Optimization
(Page 2 of 5 )

The official term for what we are aiming to achieve is ‘Return Value Optimization.’ This is a technique where you tell the compiler that it is safe to kill the temporary object needed for the returned value.

All you need to do is return a constructor argument and the compiler will take care of the rest. When an object is created directly inside a statement, without being assigned to a variable, we are looking at a constructor argument.

Lets build a sample portion of the Vector3 class as I did in the first article to visualize the compiler’s actions. We’ll implement the -= operator, the + operator and the - operator. The -= operator will demonstrate the efficiency of references, the + operator will show what happens when we don’t facilitate for return value optimization and the – operator will show the effect of return value optimization.

Our goal is to make:

Vector3 nme2ply=player.GetPos() – nmeIt->GetPos();

As efficient as:

Vector3 nme2ply(player.GetPos());
nme2ply -= nmeIt->GetPos();

To achieve this we’ll refactor the test class ‘MyClass’ into a Vector3 test class.

A Vector3 test class

#include <stdio.h>

class Vector3 {
        char const mName[80];
public:
        float mX, mY, mZ;
       
        Vector3(char const *name, float x=0.f, float y=0.f, float z=0.f)
        : mX(x), mY(y), mZ(z)
 {
  (void)sprintf(mName, “%s”, name);
  (void)printf(“%s – Constructed.\n”, mName);
 }

 Vector3(Vector3 const &other)
  : mX(other.mX), mY(other.mY), mZ(other.mZ)
 {
  (void)sprintf(mName, “’copy of %s’”, other.mName);
  (void)printf(“%s – Copy Constructed.\n”, mName);
        }
       
        ~Vector3()
        {
        (void)printf(“%s – Destructed called.\n”, mName);
        }
       
        Vector3& operator= (Vector3 const &rhs)
        {
         if (this != &rhs) {
          mX = rhs.mX;
          mY = rhs.mY;
          mZ = rhs.mZ;
        }
         (void)printf(“%s – Assigned to %s.\n”, rhs.mName, mName);
        return *this;
        }
       
        Vector3& operator-=(Vector3 const &rhs)
        {
         mX -= rhs.mX;
         mY -= rhs.mY;
         mZ -= rhs.mZ;
         (void)printf(“%s -= %s finished.\n”, mName, rhs.mName);
         return *this;
        }
       
        Vector3 const operator+ (Vector3 const &rhs) const
        {
         Vector3 result(mX+rhs.mX, mY+rhs.mY, mZ+rhs.mZ);
         (void)printf(“%s + %s finished.\n”, mName, rhs.mName);
         return result;
        }
       
        Vector3 const operator- (Vector3 const &rhs) const
        {
         (void)printf(“%s - %s finished.\n”, mName, rhs.mName);
         return Vector3(“ctor arg”, mX-rhs.mX, mY-rhs.mY, mZ-rhs.mZ);
        }
       
        void print() const
        {
         (void)printf(“%s at (%.1f %.1f %.1f)\n”, mName, mX,mY,mZ);
        }
};

You can see that operator+ uses a local variable, which is then returned by value, while the operator- uses what we now recognize as a constructor argument. We expect operator-= to be nothing less than the most efficient… but what better way than to verify this by running some code?

Note that normally it is recommended that you use std::string instead of char buffers. Here I thought it would keep the class a bit more compact, and sometimes it is just easier to use printf and sprintf instead of STL streams. Please don’t send me any emails about possible buffer overflows, as this is simply sample code. ;)


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials