C++
  Home arrow C++ arrow 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 
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


    (Page 1 of 5 )

    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.>There are only two qualities in the world: efficiency and inefficiency.

    >And only two sorts of people: the efficient and the inefficient.

     (G. Bernard Shaw 1856-1950)

    Finding the Closest Enemy

    In the previous article we discovered that there are situations where it is beneficial to return the result of a function by value. The example used was the subtraction operator implemented for a Vector3 class:

    class Vector3 {
     float mX, mY, mZ;
    public:
          ...
          Vector3 const operator– (Vector3 const &rhs) const {
      return Vector3(mX-rhs.mX, mY-rhs.mY, mZ-rhs.mZ);
          }
          ...
    };

    This operator is needed to determine a vector between two points, where the length of the vector determines the distance between the two points. It is not possible for this function to return a reference one way or the other, so it has to return the result by value.

    What we are going to examine now is how we can help the compiler to optimize the temporary variable away, so we won’t have pay a price for returning by value at all!

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class
    - Iostream Library and Basic I/O in C++
    - Introduction to Streams







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