C++
  Home arrow C++ arrow Page 2 - Temporary Variables: Chasing Temporaries A...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Temporary Variables: Chasing Temporaries Away
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2005-10-10

    Table of Contents:
  • Temporary Variables: Chasing Temporaries Away
  • Return Value Optimization
  • Vector3 test
  • Details, details… what’s that… small print?
  • Help the compiler help you

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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. ;)

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT