lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Replacing boost with std C++11 [Was: Fix value_cast defect sho


From: Vadim Zeitlin
Subject: Re: [lmi] Replacing boost with std C++11 [Was: Fix value_cast defect shown by the unit test]
Date: Wed, 11 Jan 2017 19:17:28 +0100

On Wed, 11 Jan 2017 15:33:06 +0000 Greg Chicares <address@hidden> wrote:

GC> I have two questions. First, does the patch below do this correctly?
GC> (It has the virtue of actually compiling, but I don't have a unit
GC> test to validate that it actually does the right thing.)

 I think it does work but, as you had correctly surmised, I'd rather avoid
the use of bind() in the first place.

GC> Second, I think you'll say we should use lambdas instead, and I'm
GC> not opposed, but I've been struggling with the syntax for an hour
GC> to no avail, so let me just ask how you'd do it.

 The minimal patch using lambdas:
---------------------------------- >8 --------------------------------------
diff --git a/msw_workarounds.cpp b/msw_workarounds.cpp
index a1a6831..a54d603 100644
--- a/msw_workarounds.cpp
+++ b/msw_workarounds.cpp
@@ -30,8 +30,6 @@
 #include "fenv_lmi.hpp"
 #include "handle_exceptions.hpp"
 
-#include <boost/functional.hpp>
-
 #include <windows.h>
 
 #include <algorithm>
@@ -48,7 +46,7 @@ MswDllPreloader::~MswDllPreloader()
     std::for_each
         (SuccessfullyPreloadedDlls_.begin()
         ,SuccessfullyPreloadedDlls_.end()
-        ,boost::bind1st(std::mem_fun(&MswDllPreloader::UnloadOneDll), this)
+        ,[this](std::string const& dll) { UnloadOneDll(dll); }
         );
 }
 
@@ -75,7 +73,7 @@ void MswDllPreloader::PreloadDesignatedDlls()
     std::for_each
         (std::istream_iterator<std::string>(iss)
         ,std::istream_iterator<std::string>()
-        ,boost::bind1st(std::mem_fun(&MswDllPreloader::PreloadOneDll), this)
+        ,[this](std::string const& dll) { PreloadOneDll(dll); }
         );
     fenv_initialize();
 }
---------------------------------- >8 --------------------------------------

 But, frankly, this still seems unnecessarily heavy to me and my preferred
version would be:
---------------------------------- >8 --------------------------------------
diff --git a/msw_workarounds.cpp b/msw_workarounds.cpp
index a1a6831..41d0789 100644
--- a/msw_workarounds.cpp
+++ b/msw_workarounds.cpp
@@ -30,12 +30,8 @@
 #include "fenv_lmi.hpp"
 #include "handle_exceptions.hpp"
 
-#include <boost/functional.hpp>
-
 #include <windows.h>
 
-#include <algorithm>
-#include <functional>
 #include <iterator>
 #include <sstream>
 
@@ -45,11 +41,10 @@ MswDllPreloader::MswDllPreloader()
 
 MswDllPreloader::~MswDllPreloader()
 {
-    std::for_each
-        (SuccessfullyPreloadedDlls_.begin()
-        ,SuccessfullyPreloadedDlls_.end()
-        ,boost::bind1st(std::mem_fun(&MswDllPreloader::UnloadOneDll), this)
-        );
+    for(auto const& dll: SuccessfullyPreloadedDlls_)
+        {
+        UnloadOneDll(dll);
+        }
 }
 
 MswDllPreloader& MswDllPreloader::instance()
@@ -72,11 +67,11 @@ void MswDllPreloader::PreloadDesignatedDlls()
     std::istringstream iss
         (configurable_settings::instance().libraries_to_preload()
         );
-    std::for_each
-        (std::istream_iterator<std::string>(iss)
-        ,std::istream_iterator<std::string>()
-        ,boost::bind1st(std::mem_fun(&MswDllPreloader::PreloadOneDll), this)
-        );
+    using iter = std::istream_iterator<std::string>;
+    for(auto i = iter(iss); i != iter(); ++i)
+        {
+        PreloadOneDll(*i);
+        }
     fenv_initialize();
 }
 
---------------------------------- >8 --------------------------------------

 It's unfortunate that the range-based for loop can't be used with
std::istream, if we had some kind of split_into_words() function we could
use it here, but it doesn't seem to be worth adding it just for this.
Regardless, even an explicit loop like above is IMHO preferable to
std::for_each() which, I believe, should never be used in C++11, it just
obfuscates things instead of simplifying them. 

 Regards,
VZ


reply via email to

[Prev in Thread] Current Thread [Next in Thread]