Hi,

On 07/23/2013 12:45 PM, Chung-Ju Wu wrote:
2013/7/23 Paolo Carlini <paolo.carl...@oracle.com>:
... in the future, when we manage to actually avoid including <cstdio> from
<string> I think we can avoid including it from <random> if we play a bit
with void*. Should be safe aliasing-wise. Something like the below,
completely untested.
I tested it on my target.  At least the elf toolchain can be
successfully built with such changes.
Thanks for testing! I guess we can as well do this now that we are still in Stage 1. Also, take the chance to avoid a couple of unnecessary reinterpret_casts. Like in the below, which I'm going to apply if testing on x86_64-linux goes well.

Paolo.

///////////////////
2013-07-24  Paolo Carlini  <paolo.carl...@oracle.com>

        * include/bits/random.h (random_device): Avoid using the FILE type.
        * include/std/random: Do not include <cstdio>.
        * src/c++11/random.cc: Include it here.
        (random_device::_M_init, random_device::_M_fini,
        random_device::_M_getval): Cast back and forth void* and FILE*.
Index: include/bits/random.h
===================================================================
--- include/bits/random.h       (revision 201200)
+++ include/bits/random.h       (working copy)
@@ -1638,10 +1638,10 @@
 
     union
     {
-    FILE*        _M_file;
-    mt19937      _M_mt;
+      void*      _M_file;
+      mt19937    _M_mt;
+    };
   };
-  };
 
   /* @} */ // group random_generators
 
Index: include/std/random
===================================================================
--- include/std/random  (revision 201200)
+++ include/std/random  (working copy)
@@ -36,7 +36,6 @@
 #else
 
 #include <cmath>
-#include <cstdio> // For FILE
 #include <cstdlib>
 #include <string>
 #include <iosfwd>
Index: src/c++11/random.cc
===================================================================
--- src/c++11/random.cc (revision 201202)
+++ src/c++11/random.cc (working copy)
@@ -30,13 +30,14 @@
 # include <cpuid.h>
 #endif
 
+#include <cstdio>
+
 #ifdef _GLIBCXX_HAVE_UNISTD_H
 # include <unistd.h>
 #endif
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
-
   namespace
   {
     static unsigned long
@@ -72,7 +73,6 @@
 #endif
   }
 
-
   void
   random_device::_M_init(const std::string& token)
   {
@@ -102,8 +102,8 @@
       std::__throw_runtime_error(__N("random_device::"
                                     "random_device(const std::string&)"));
 
-    _M_file = std::fopen(fname, "rb");
-    if (! _M_file)
+    _M_file = static_cast<void*>(std::fopen(fname, "rb"));
+    if (!_M_file)
       goto fail;
   }
 
@@ -117,23 +117,24 @@
   random_device::_M_fini()
   {
     if (_M_file)
-      std::fclose(_M_file);
+      std::fclose(static_cast<FILE*>(_M_file));
   }
 
   random_device::result_type
   random_device::_M_getval()
   {
 #if (defined __i386__ || defined __x86_64__) && defined _GLIBCXX_X86_RDRAND
-    if (! _M_file)
+    if (!_M_file)
       return __x86_rdrand();
 #endif
 
     result_type __ret;
 #ifdef _GLIBCXX_HAVE_UNISTD_H
-    read(fileno(_M_file), reinterpret_cast<void*>(&__ret), 
sizeof(result_type));
+    read(fileno(static_cast<FILE*>(_M_file)),
+        static_cast<void*>(&__ret), sizeof(result_type));
 #else
-    std::fread(reinterpret_cast<void*>(&__ret), sizeof(result_type),
-              1, _M_file);
+    std::fread(static_cast<void*>(&__ret), sizeof(result_type),
+              1, static_cast<FILE*>(_M_file));
 #endif
     return __ret;
   }

Reply via email to