You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Farid Zaripov <Fa...@epam.com> on 2007/03/07 16:52:51 UTC

[PATCH] dynatype.cpp

  Below is a patch of dynatype.cpp example to get compiled on MSVC.

  ChangeLog:
  * dynatype.cpp: protortype dynatype::remove<dynatype::map<void> >
  changed to dynatype::remove<void>;
  protortype dynatype::copy<dynatype::map<void> > changed to
  dynatype::copy<void>;
  removed dynatype::operator=<dynatype::map<void> >;
  added code to get compiled on MSVC

===========================================
Index: dynatype.cpp
===================================================================
--- dynatype.cpp	(revision 515614)
+++ dynatype.cpp	(working copy)
@@ -2,7 +2,7 @@
  *
  * dynatype.cpp - Example program of map. See Class Reference Section
  *
- * $Id: //stdlib/dev/examples/stdlib/tutorial/dynatype.cpp#11 $
+ * $Id$
  *
 
************************************************************************
***
  *
@@ -105,27 +105,19 @@
 
 // 14.7.3, p6 - explicit specializations must be defined before first
use
 template <>
-inline void dynatype::remove<dynatype::map<void> >()
+inline void dynatype::remove<void>()
 { /* no-op */ }
 
 
 template <>
-inline void dynatype::copy<dynatype::map<void> >(const dynatype&)
+inline void dynatype::copy<void>(const dynatype&)
 { /* no-op */ }
 
 
-template <>
-inline dynatype& dynatype::operator= (const dynatype::map<void>&)
-{
-    // no-op
-    return *this;
-}
-
-
 // initialize with pointers to no-ops
 inline dynatype::dynatype ()
-    : p_remove (&dynatype::remove<map<void> >),
-      p_copy (&dynatype::copy<map<void> >)
+    : p_remove (&dynatype::remove<void>),
+      p_copy (&dynatype::copy<void>)
 {
 }
 
@@ -176,8 +168,30 @@
 }
 
 
+#if defined (_MSC_VER) && (_MSC_VER <= 1310)
+
+// to avoid MSVC 7.1 error LNK2019: unresolved external symbol
+static inline void foo ()
+{
+    dynatype dt;
+    int         &i = dt;
+    double      &d = dt;
+    const char *&p = dt;
+    char        &c = dt;
+}
+
+#endif  // (_MSC_VER) && (_MSC_VER <= 1310)
+
+
 int main ()
 {
+    // avoid error C2440 on MSVC
+#ifdef _MSC_VER
+#define CREF(x) const_cast<const dynatype &>(x)
+#else
+#define CREF(x) x
+#endif
+
     try {
         std::cout << "dynatype v1 = 1\n";
 
@@ -185,32 +199,32 @@
         dynatype v1 = 1;
 
         std::cout << "int (v1) = "
-                  << int (v1) << std::endl;
+                  << int (CREF (v1)) << std::endl;
 
         // assign a double to an instance of dynatype
         v1 = 3.14;
 
         std::cout << "double (v1 = 3.14) = "
-                  << double (v1) << std::endl;
+                  << double (CREF (v1)) << std::endl;
 
         // copy construct an instance of dynatype
         dynatype v2 = v1;
 
         std::cout << "double (v2 = v1) = "
-                  << double (v2) << std::endl;
+                  << double (CREF (v2)) << std::endl;
 
         // 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;
+                  << (const char*)CREF (v2) << std::endl;
 
         // assign one instance of dynatype to another
         v1 = v2;
 
         std::cout << "(const char*)(v1 = v2) = "
-                  << (const char*)v1 << std::endl;
+                  << (const char*)CREF (v1) << std::endl;
 
         // create uninitialized (untyped) instances of dynatype
         dynatype v3, v4;
@@ -220,7 +234,7 @@
 
         // attempt to extract any value from an unitialized dynatype
fails
         std::cout << "char (v3) = "
-                  << char (v1) << std::endl;
+                  << char (CREF (v3)) << std::endl;
         
     }
     catch (...) {
===========================================

Farid.

Re: [PATCH] dynatype.cpp

Posted by Martin Sebor <se...@roguewave.com>.
Farid Zaripov wrote:
>   Below is a patch of dynatype.cpp example to get compiled on MSVC.
> 
>   ChangeLog:
>   * dynatype.cpp: protortype dynatype::remove<dynatype::map<void> >
>   changed to dynatype::remove<void>;
>   protortype dynatype::copy<dynatype::map<void> > changed to
>   dynatype::copy<void>;
>   removed dynatype::operator=<dynatype::map<void> >;
>   added code to get compiled on MSVC
> 
> ===========================================
> Index: dynatype.cpp
> ===================================================================
> --- dynatype.cpp	(revision 515614)
> +++ dynatype.cpp	(working copy)
> @@ -2,7 +2,7 @@
>   *
>   * dynatype.cpp - Example program of map. See Class Reference Section
>   *
> - * $Id: //stdlib/dev/examples/stdlib/tutorial/dynatype.cpp#11 $
> + * $Id$
>   *
>  
> ************************************************************************
> ***
>   *
> @@ -105,27 +105,19 @@
>  
>  // 14.7.3, p6 - explicit specializations must be defined before first
> use
>  template <>
> -inline void dynatype::remove<dynatype::map<void> >()
> +inline void dynatype::remove<void>()

The replacement doesn't look equivalent to the original. Is it correct
for compilers other than MSVC?

>  { /* no-op */ }
>  
>  
>  template <>
> -inline void dynatype::copy<dynatype::map<void> >(const dynatype&)
> +inline void dynatype::copy<void>(const dynatype&)
>  { /* no-op */ }
>  
>  
> -template <>
> -inline dynatype& dynatype::operator= (const dynatype::map<void>&)
> -{
> -    // no-op
> -    return *this;
> -}
> -
> -
>  // initialize with pointers to no-ops
>  inline dynatype::dynatype ()
> -    : p_remove (&dynatype::remove<map<void> >),
> -      p_copy (&dynatype::copy<map<void> >)
> +    : p_remove (&dynatype::remove<void>),
> +      p_copy (&dynatype::copy<void>)
>  {
>  }
>  
> @@ -176,8 +168,30 @@
>  }
>  
>  
> +#if defined (_MSC_VER) && (_MSC_VER <= 1310)

The examples should be portable C++, i.e., free of #ifdef's and other
platform-specific workarounds. Unless there's a way to get it to work
with the compiler I'm not sure we should pollute the code with hacks
for broken compilers. Btw., do you have a test case for the MSVC bug
that prevents the code from compiling?

Martin

> +
> +// to avoid MSVC 7.1 error LNK2019: unresolved external symbol
> +static inline void foo ()
> +{
> +    dynatype dt;
> +    int         &i = dt;
> +    double      &d = dt;
> +    const char *&p = dt;
> +    char        &c = dt;
> +}
> +
> +#endif  // (_MSC_VER) && (_MSC_VER <= 1310)
> +
> +
>  int main ()
>  {
> +    // avoid error C2440 on MSVC
> +#ifdef _MSC_VER
> +#define CREF(x) const_cast<const dynatype &>(x)
> +#else
> +#define CREF(x) x
> +#endif
> +
>      try {
>          std::cout << "dynatype v1 = 1\n";
>  
> @@ -185,32 +199,32 @@
>          dynatype v1 = 1;
>  
>          std::cout << "int (v1) = "
> -                  << int (v1) << std::endl;
> +                  << int (CREF (v1)) << std::endl;
>  
>          // assign a double to an instance of dynatype
>          v1 = 3.14;
>  
>          std::cout << "double (v1 = 3.14) = "
> -                  << double (v1) << std::endl;
> +                  << double (CREF (v1)) << std::endl;
>  
>          // copy construct an instance of dynatype
>          dynatype v2 = v1;
>  
>          std::cout << "double (v2 = v1) = "
> -                  << double (v2) << std::endl;
> +                  << double (CREF (v2)) << std::endl;
>  
>          // 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;
> +                  << (const char*)CREF (v2) << std::endl;
>  
>          // assign one instance of dynatype to another
>          v1 = v2;
>  
>          std::cout << "(const char*)(v1 = v2) = "
> -                  << (const char*)v1 << std::endl;
> +                  << (const char*)CREF (v1) << std::endl;
>  
>          // create uninitialized (untyped) instances of dynatype
>          dynatype v3, v4;
> @@ -220,7 +234,7 @@
>  
>          // attempt to extract any value from an unitialized dynatype
> fails
>          std::cout << "char (v3) = "
> -                  << char (v1) << std::endl;
> +                  << char (CREF (v3)) << std::endl;
>          
>      }
>      catch (...) {
> ===========================================
> 
> Farid.
> 
>