C++
  Home arrow C++ arrow Temporary Variables: Runtime rvalue Detect...
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: Runtime rvalue Detection
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2005-10-17

    Table of Contents:
  • Temporary Variables: Runtime rvalue Detection
  • The Ternary Conditional Operator
  • The RVALUE_TEST macro
  • Determining Type

  • 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: Runtime rvalue Detection


    (Page 1 of 4 )

    In this final article in the series covering temporary variables, Jun Nakamura explains how you can test whether a variable is temporary or not, by using the ternary conditional operator.

     The more precisely the position is determined,

    The less precisely the momentum is known in this instant,

    And vice versa.

    [Heisenberg, uncertainty paper, 1927]

    C++ Beyond the limits

    Each time when I think that something is simple and easy to understand, I discover there are new depths to explore. The previous four articles touched upon a "simple" subject: Temporary Variables –- and I now consider topics only to be as simple as you want them to be.

    Reading other articles and books, I came across the terms lvalue and rvalue. Quickly I discovered these were the official terms for what I had previously known as temporary variables or even unnamed objects. To fully understand their effect it was quite useful to write some simple test code in order to make them appear so we could gain some insight into when the compiler creates and destroys them.

    Then you discover there are no single simple solutions to make the problems go away. Whenever a solution appears, another problem appears with it hand in hand. This makes sense of course: if there were a simple solution, the Standards Committee would have probably enforced it long ago. So from "Object Slicing" to "Return Value Optimization" it becomes clear that there is always more than meets the eye.

    Some time ago I stumbled upon an article that showed me how deep this temporary rabbit hole can really go. It is an excellent article written by Eric Niebler: “Conditional Love: FOREACH Redux” [Niebler]. This article explains how the BOOST_FOREACH macro works and which tricks have to be pulled to make it work.

    Since I have been approaching rvalues from different angles in these articles, I wanted to conclude with an explanation of how you can test whether a variable is temporary or not. The following is an abstract of Eric Niebler’s article and explains how you can test whether a variable is an rvalue or not by using the ternary conditional operator.

    Please note that the following code was tested successfully on gcc 3.3.3 but doesn’t work with the Visual C++ Compiler.

    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 5 hosted by Hostway
    Stay green...Green IT