C++
  Home arrow C++ arrow Page 3 - 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 
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 - The RVALUE_TEST macro


    (Page 3 of 4 )

    Niebler has found a smart way to use the characteristics of the ternary operator to determine whether something is an rvalue or not. First let's have a look at the macro:

    #define RVALUE_TEST( container ) \
     try { ( true ? rvalue_probe() : (container) ); } \
     catch ( char const *result ) { cout << result << ‘\n’; }

    It probably looks a bit peculiar because the conditional expression is missing; instead the condition has already been evaluated to true. The trick here is that the ternary operator is not used in its regular way, but to test whether container is an rvalue or not.

    So what kind of object is the rvalue_probe?

    struct rvalue_probe {
     template < class R > operator R () { throw “rvalue”; }
     template < class L > operator L&() const { throw “lvalue”; }
    }

    This code makes it possible to print "rvalue" to stdout when evaluating RVALUE_TEST(“test”) and "lvalue" to stdout when evaluating RVALUE_TEST(cout); ! To understand how the ternary operator evaluates the expression in the macro we need to take a closer look at its definition in the C++ Standard.

    More C++ Articles
    More By J. Nakamura


     

    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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek