[libcxx-commits] [PATCH] D107663: [libc++] Restore `basic_ios`'s implicit conversion to `bool` in C++03 mode.

Arthur O'Dwyer via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 6 12:23:34 PDT 2021


Quuxplusone created this revision.
Quuxplusone added reviewers: ldionne, efriedma, libc++.
Quuxplusone added a project: libc++.
Quuxplusone requested review of this revision.
Herald added a subscriber: libcxx-commits.
Herald added 1 blocking reviewer(s): libc++.

@efriedma noted that my D104682 <https://reviews.llvm.org/D104682> broke this test case, reduced from SPEC2006.

      
  #include <istream>
  bool a(std::istream a) {
      return a.getline(0,0) == 0;
  }
      

We can unbreak it by restoring the conversion to something-convertible-to-bool. I chose `const void*` to match libstdc++ (which uses non-const `void*`). We could of course just choose `bool` itself, but that would break some existing libc++ tests which assert `basic_ios` is NOT convertible to `int`.

I'm not at all sure that we want this patch; it might be better to say that in the year 2021 we no longer support a benchmark from 2006 written in a language from 2003, and those people should just fix their code already. But //if// we want it, then I think this is what we want. @efriedma, what are your thoughts re: the appropriate libc++ action here?


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107663

Files:
  libcxx/include/ios
  libcxx/test/std/input.output/iostreams.base/ios/iostate.flags/bool.pass.cpp


Index: libcxx/test/std/input.output/iostreams.base/ios/iostate.flags/bool.pass.cpp
===================================================================
--- libcxx/test/std/input.output/iostreams.base/ios/iostate.flags/bool.pass.cpp
+++ libcxx/test/std/input.output/iostreams.base/ios/iostate.flags/bool.pass.cpp
@@ -27,7 +27,12 @@
     static_assert((!std::is_convertible<std::ios, void*>::value), "");
     static_assert((!std::is_convertible<std::ios, int>::value), "");
     static_assert((!std::is_convertible<std::ios const&, int>::value), "");
-    static_assert((!std::is_convertible<std::ios, bool>::value), "");
+#if TEST_STD_VER >= 11
+    static_assert(!std::is_convertible<std::ios, bool>::value, "");
+#else
+    static_assert(std::is_convertible<std::ios, bool>::value, "");
+    (void)(ios == 0);  // SPEC2006 apparently relies on this to compile
+#endif
 
   return 0;
 }
Index: libcxx/include/ios
===================================================================
--- libcxx/include/ios
+++ libcxx/include/ios
@@ -607,8 +607,15 @@
     static_assert((is_same<_CharT, typename traits_type::char_type>::value),
                   "traits_type::char_type must be the same type as CharT");
 
+#ifdef _LIBCPP_CXX03_LANG
+    // Preserve the ability to compare with literal 0,
+    // and implicitly convert to bool, but not implicitly convert to int.
+    _LIBCPP_INLINE_VISIBILITY
+    operator const void*() const {return fail() ? nullptr : this;}
+#else
     _LIBCPP_INLINE_VISIBILITY
     explicit operator bool() const {return !fail();}
+#endif
 
     _LIBCPP_INLINE_VISIBILITY bool operator!() const    {return  fail();}
     _LIBCPP_INLINE_VISIBILITY iostate rdstate() const   {return ios_base::rdstate();}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107663.364866.patch
Type: text/x-patch
Size: 1726 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210806/dd53d90b/attachment.bin>


More information about the libcxx-commits mailing list