lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 30581cb 3/4: Make value_cast<bool>("0") retur


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 30581cb 3/4: Make value_cast<bool>("0") return false (VZ)
Date: Sun, 8 Jan 2017 18:02:24 +0000 (UTC)

branch: master
commit 30581cbc57048608a5f81bbbdc5c25711c65de1a
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Make value_cast<bool>("0") return false (VZ)
    
    This fixes a unit test that was previously (probably mistakenly)
    disabled for gcc 4.1 and was broken again with gcc 6.3 and makes the
    previously disabled unit test checking that converting "0" to bool
    using value_cast<> yields the expected "false", and not "true" as
    before, pass. See:
      http://lists.nongnu.org/archive/html/lmi/2017-01/msg00021.html
---
 value_cast.hpp |   21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/value_cast.hpp b/value_cast.hpp
index 6d6c861..9bb0a10 100644
--- a/value_cast.hpp
+++ b/value_cast.hpp
@@ -176,6 +176,20 @@ To numeric_value_cast(From const& from)
 
 /// Class template value_cast_choice is an appurtenance of function
 /// template value_cast(); it selects the best conversion method.
+///
+/// The choice among conversion methods depends in part on whether
+/// an implicit conversion is available. Implicit conversions from
+/// pointer or array to bool are disregarded as being infelicitous.
+/// For example, given:
+///   char const* untrue = "0";
+/// these casts:
+///   static_cast<bool>(untrue);  // converts pointer->bool
+///   static_cast<bool>("0");     // converts array->pointer->bool
+/// return 'true' because the conversions involve non-null pointers;
+/// however, these casts:
+///   value_cast<bool>(untrue);
+///   value_cast<bool>("0");
+/// preserve the value by returning 'false'.
 
 template<typename To, typename From>
 struct value_cast_choice
@@ -183,7 +197,10 @@ struct value_cast_choice
     enum
         {
         // Here, is_convertible means 'From' is convertible to 'To'.
-        convertible = std::is_convertible<From,To>::value
+        felicitously_convertible =
+                std::is_convertible<From,To>::value
+            &&!(std::is_array   <From>::value && std::is_same<bool,To>::value)
+            &&!(std::is_pointer <From>::value && std::is_same<bool,To>::value)
         };
 
     enum
@@ -202,7 +219,7 @@ struct value_cast_choice
 
     enum
         {choice =
-            convertible
+            felicitously_convertible
                 ?both_numeric
                     ?e_both_numeric
                     :e_direct



reply via email to

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