C++
  Home arrow C++ arrow Page 5 - 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 - Help the compiler help you


    (Page 5 of 5 )

    Hopefully you will find it easy to keep an eye on the possible creation and destruction of temporary objects now. Of course it would be nice to have some support as well… you only need to forget once to add ‘&’ to your const function parameters and you are passing by value all of a sudden! You might think this won’t happen to you, but I found this happening to me when jumping between programming languages a lot. It was particularly noticeable when coming back from a scripting language like Python or LUA to C++.

    Many scripting languages pass their variables around by reference as a default, so there is no special ‘reference’ operator like in C++. What if your compiler could tell you that you are passing by value when you probably don’t want this?

    It is actually quite easy: make your copy constructor and assignment operator private by default. Now you won’t receive the ‘free’ bitwise copy constructor from the compiler, which it needs to create temporary objects. There are many situations where you want to be able to create copies of your class of course (when using STL containers for example), but I find it a good practice to make everything non-copyable by default and only explicitly allow for copies when needed, instead of the other way around.

    class Skeleton {
    private:
     // copy ctor and assign operators are hidden by default.
     Skeleton(Skeleton const&);
     Skeleton& operator=(Skeleton const&);
    };

    Next time we'll take a walk on the wild side… detecting rvalues at runtime!

    References

     [Meyers] – Scott Meyers

    “Effective C++” – ISBN 0201924889

    [Sutter] – Herb Sutter

    “Exceptional C++” -  ISBN 0201615622

    [Niebler] – Eric Niebler

    “Conditional Love: FOREACH Redux”

    http://www.artima.com/cppsource/foreach.html

    [Kernighan] – Brian Kernighan (interview)

    http://www-2.cs.cmu.edu/~mihaib/kernighan-interview/

     
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







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