stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vi...@apache.org
Subject svn commit: r610576 - in /incubator/stdcxx/trunk/examples/tutorial: dynatype.cpp out/dynatype.out
Date Wed, 09 Jan 2008 21:34:00 GMT
Author: vitek
Date: Wed Jan  9 13:33:55 2008
New Revision: 610576

URL: http://svn.apache.org/viewvc?rev=610576&view=rev
Log:

2008-01-08  Travis Vitek  <vitek@roguewave.com>

    STDCXX-605
    * examples/tutorial/dynatype.cpp: Add overloaded method
    that discards const to avoid looking in wrong map when
    non-const conversion operator is used with a const type.
    (main): Display text and converted value in two separate
    statements to ensure output is generated the same on all
    platforms. Add a return 0 for correctness.
    * examples/tutorial/out/dynatype.out: Update output file
    to match expected output format.


Modified:
    incubator/stdcxx/trunk/examples/tutorial/dynatype.cpp
    incubator/stdcxx/trunk/examples/tutorial/out/dynatype.out

Modified: incubator/stdcxx/trunk/examples/tutorial/dynatype.cpp
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/examples/tutorial/dynatype.cpp?rev=610576&r1=610575&r2=610576&view=diff
==============================================================================
--- incubator/stdcxx/trunk/examples/tutorial/dynatype.cpp (original)
+++ incubator/stdcxx/trunk/examples/tutorial/dynatype.cpp Wed Jan  9 13:33:55 2008
@@ -64,6 +64,20 @@
     void (dynatype::*p_remove)();
     void (dynatype::*p_copy)(const dynatype&);
 
+    // provides access to the mapped data value or throws bad_cast
+    template <class T>
+    T& retrieve (const T*) {
+        if (map<T>::get ().end () == map<T>::get ().find (this))
+            throw std::bad_cast ();
+        return map<T>::get () [this];
+    }
+
+    // overload throws away const on template parameter
+    template <class T>
+    T& retrieve (const T*) const {
+        return const_cast<dynatype*>(this)->retrieve ((T*)0);
+    }
+
 public:
 
     dynatype ();
@@ -91,16 +105,14 @@
     // dynatype throws std::bad_cast if the types don't match exactly
     template <class T>
     operator T& () {
-        if (map<T>::get ().end () == map<T>::get ().find (this))
-            throw std::bad_cast ();
-        return map<T>::get () [this];
+        return retrieve ((T*)0);
     }
 
     // retrieve a const reference to the concrete type from an instance of
     // dynatype throws std::bad_cast if the types don't match exactly
     template <class T>
     operator const T& () const {
-        return static_cast<T&> (*const_cast<dynatype*> (this));
+        return retrieve ((T*)0);
     }
 
     // assign a value of any type to an instance of dynatype
@@ -194,38 +206,37 @@
 int main ()
 {
     try {
-        std::cout << "dynatype v1 = 1\n";
 
         // create an instance of dynatype an initialized it with an int
         dynatype v1 = 1;
 
-        std::cout << "int (v1) = "
-                  << int (v1) << std::endl;
+        std::cout << "static_cast<const int&>(v1 = 1) = ";
+        std::cout <<  static_cast<const int&>(v1) << '\n';
 
         // assign a double to an instance of dynatype
         v1 = 3.14;
 
-        std::cout << "double (v1 = 3.14) = "
-                  << double (v1) << std::endl;
+        std::cout << "static_cast<const double&>(v1 = 3.14) = ";
+        std::cout <<  static_cast<const double&>(v1) << '\n';
 
         // copy construct an instance of dynatype
         dynatype v2 = v1;
 
-        std::cout << "double (v2 = v1) = "
-                  << double (v2) << std::endl;
+        std::cout << "static_cast<const double&>(v2 = v1) = ";
+        std::cout <<  static_cast<const double&>(v2) << '\n';
 
         // assign a const char* literal to an instance of dynatype
         const char* const literal = "abc";
         v2 = literal;
 
-        std::cout << "(const char*)(v2 = \"abc\") = "
-                  << (const char*)v2 << std::endl;
+        std::cout << "static_cast<const char* const&>(v2 = \"abc\") = ";
+        std::cout <<  static_cast<const char* const&>(v2) << '\n';
 
         // assign one instance of dynatype to another
         v1 = v2;
 
-        std::cout << "(const char*)(v1 = v2) = "
-                  << (const char*)v1 << std::endl;
+        std::cout << "static_cast<const char* const&>(v1 = v2) = ";
+        std::cout <<  static_cast<const char* const&>(v1) << '\n';
 
         // create uninitialized (untyped) instances of dynatype
         dynatype v3, v4;
@@ -234,11 +245,13 @@
         v3 = v4;
 
         // attempt to extract any value from an unitialized dynatype fails
-        std::cout << "char (v3) = "
-                  << char (v3) << std::endl;
+        std::cout << "static_cast<const char&>(v3) = ";
+        std::cout <<  static_cast<const char&>(v3) << '\n';
         
     }
     catch (...) {
         std::cerr << "exception\n";
     }
+
+    return 0;
 }

Modified: incubator/stdcxx/trunk/examples/tutorial/out/dynatype.out
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/examples/tutorial/out/dynatype.out?rev=610576&r1=610575&r2=610576&view=diff
==============================================================================
--- incubator/stdcxx/trunk/examples/tutorial/out/dynatype.out (original)
+++ incubator/stdcxx/trunk/examples/tutorial/out/dynatype.out Wed Jan  9 13:33:55 2008
@@ -1,7 +1,6 @@
-dynatype v1 = 1
-int (v1) = 1
-double (v1 = 3.14) = 3.14
-double (v2 = v1) = 3.14
-(const char*)(v2 = "abc") = abc
-(const char*)(v1 = v2) = abc
-exception
+static_cast<const int&>(v1 = 1) = 1
+static_cast<const double&>(v1 = 3.14) = 3.14
+static_cast<const double&>(v2 = v1) = 3.14
+static_cast<const char* const&>(v2 = "abc") = abc
+static_cast<const char* const&>(v1 = v2) = abc
+static_cast<const char&>(v3) = exception



Mime
View raw message