[libc++] Enable `explicit` conversion operators, even in C++03 mode.
authorArthur O'Dwyer <arthur.j.odwyer@gmail.com>
Tue, 15 Jun 2021 16:57:54 +0000 (12:57 -0400)
committerArthur O'Dwyer <arthur.j.odwyer@gmail.com>
Tue, 22 Jun 2021 17:35:59 +0000 (13:35 -0400)
C++03 didn't support `explicit` conversion operators;
but Clang's C++03 mode does, as an extension, so we can use it.
This lets us make the conversion explicit in `std::function` (even in '03),
and remove some silly metaprogramming in `std::basic_ios`.

Drive-by improvements to the tests for these operators, in addition
to making sure all these tests also run in `c++03` mode.

Differential Revision: https://reviews.llvm.org/D104682

19 files changed:
libcxx/include/__config
libcxx/include/__functional_03
libcxx/include/__memory/shared_ptr.h
libcxx/include/__memory/unique_ptr.h
libcxx/include/__mutex_base
libcxx/include/exception
libcxx/include/functional
libcxx/include/ios
libcxx/include/istream
libcxx/include/ostream
libcxx/include/system_error
libcxx/test/std/diagnostics/syserr/syserr.errcode/syserr.errcode.observers/bool.compile.fail.cpp [deleted file]
libcxx/test/std/diagnostics/syserr/syserr.errcode/syserr.errcode.observers/bool.pass.cpp
libcxx/test/std/input.output/iostreams.base/ios/iostate.flags/bool.pass.cpp
libcxx/test/std/language.support/support.exception/propagation/exception_ptr.pass.cpp
libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.shared/thread.lock.shared.obs/op_bool.pass.cpp
libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.obs/op_bool.pass.cpp
libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.cap/operator_bool.pass.cpp
libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/op_bool.pass.cpp

index abeb9a7..a408382 100644 (file)
@@ -874,13 +874,6 @@ typedef unsigned int   char32_t;
 #  define _LIBCPP_NOALIAS
 #endif
 
-#if __has_feature(cxx_explicit_conversions) || defined(__IBMCPP__) || \
-    (!defined(_LIBCPP_CXX03_LANG) && defined(__GNUC__)) // All supported GCC versions
-#  define _LIBCPP_EXPLICIT explicit
-#else
-#  define _LIBCPP_EXPLICIT
-#endif
-
 #if __has_attribute(using_if_exists)
 # define _LIBCPP_USING_IF_EXISTS __attribute__((using_if_exists))
 #else
index c918fdf..619d92e 100644 (file)
@@ -493,7 +493,7 @@ public:
         {function(allocator_arg, __a, __f).swap(*this);}
 
     // 20.7.16.2.3, function capacity:
-    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
+    _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
 
 private:
     // deleted overloads close possible hole in the type system
@@ -773,7 +773,7 @@ public:
         {function(allocator_arg, __a, __f).swap(*this);}
 
     // 20.7.16.2.3, function capacity:
-    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
+    _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
 
 private:
     // deleted overloads close possible hole in the type system
@@ -1053,7 +1053,7 @@ public:
         {function(allocator_arg, __a, __f).swap(*this);}
 
     // 20.7.16.2.3, function capacity:
-    operator bool() const {return __f_;}
+    _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
 
 private:
     // deleted overloads close possible hole in the type system
@@ -1332,7 +1332,7 @@ public:
         {function(allocator_arg, __a, __f).swap(*this);}
 
     // 20.7.16.2.3, function capacity:
-    _LIBCPP_INLINE_VISIBILITY operator bool() const {return __f_;}
+    _LIBCPP_INLINE_VISIBILITY explicit operator bool() const {return __f_;}
 
 private:
     // deleted overloads close possible hole in the type system
index f325362..ab92651 100644 (file)
@@ -588,7 +588,7 @@ public:
     _LIBCPP_INLINE_VISIBILITY
     bool unique() const _NOEXCEPT {return use_count() == 1;}
     _LIBCPP_INLINE_VISIBILITY
-    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != nullptr;}
+    explicit operator bool() const _NOEXCEPT {return get() != nullptr;}
     template <class _Up>
         _LIBCPP_INLINE_VISIBILITY
         bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
index 7585a91..9442e54 100644 (file)
@@ -296,7 +296,7 @@ public:
     return __ptr_.second();
   }
   _LIBCPP_INLINE_VISIBILITY
-  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
+  explicit operator bool() const _NOEXCEPT {
     return __ptr_.first() != nullptr;
   }
 
@@ -517,7 +517,7 @@ public:
     return __ptr_.second();
   }
   _LIBCPP_INLINE_VISIBILITY
-  _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
+  explicit operator bool() const _NOEXCEPT {
     return __ptr_.first() != nullptr;
   }
 
index 121973d..77590a8 100644 (file)
@@ -189,8 +189,7 @@ public:
     _LIBCPP_INLINE_VISIBILITY
     bool owns_lock() const _NOEXCEPT {return __owns_;}
     _LIBCPP_INLINE_VISIBILITY
-    _LIBCPP_EXPLICIT
-        operator bool () const _NOEXCEPT {return __owns_;}
+    explicit operator bool() const _NOEXCEPT {return __owns_;}
     _LIBCPP_INLINE_VISIBILITY
     mutex_type* mutex() const _NOEXCEPT {return __m_;}
 };
index 3405a42..353ebf7 100644 (file)
@@ -151,7 +151,7 @@ public:
     exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
     ~exception_ptr() _NOEXCEPT;
 
-    _LIBCPP_INLINE_VISIBILITY _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
+    _LIBCPP_INLINE_VISIBILITY explicit operator bool() const _NOEXCEPT
     {return __ptr_ != nullptr;}
 
     friend _LIBCPP_INLINE_VISIBILITY
@@ -205,7 +205,7 @@ public:
     exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT;
     exception_ptr& operator=(nullptr_t) _NOEXCEPT;
     ~exception_ptr() _NOEXCEPT;
-    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT;
+    explicit operator bool() const _NOEXCEPT;
 };
 
 _LIBCPP_FUNC_VIS
index 193173e..68ed22e 100644 (file)
@@ -2111,7 +2111,7 @@ template <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
     }
 
     _LIBCPP_INLINE_VISIBILITY
-    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != nullptr; }
+    explicit operator bool() const _NOEXCEPT { return __f_ != nullptr; }
 
 #ifndef _LIBCPP_NO_RTTI
     _LIBCPP_INLINE_VISIBILITY
@@ -2597,7 +2597,7 @@ public:
 
     // function capacity:
     _LIBCPP_INLINE_VISIBILITY
-    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
+    explicit operator bool() const _NOEXCEPT {
       return static_cast<bool>(__f_);
     }
 
index c58b983..eefb58f 100644 (file)
@@ -591,13 +591,6 @@ ios_base::exceptions(iostate __iostate)
     clear(__rdstate_);
 }
 
-#if defined(_LIBCPP_CXX03_LANG)
-struct _LIBCPP_TYPE_VIS __cxx03_bool {
-  typedef void (__cxx03_bool::*__bool_type)();
-  void __true_value() {}
-};
-#endif
-
 template <class _CharT, class _Traits>
 class _LIBCPP_TEMPLATE_VIS basic_ios
     : public ios_base
@@ -614,18 +607,8 @@ public:
     static_assert((is_same<_CharT, typename traits_type::char_type>::value),
                   "traits_type::char_type must be the same type as CharT");
 
-  // __true_value will generate undefined references when linking unless
-  // we give it internal linkage.
-
-#if defined(_LIBCPP_CXX03_LANG)
     _LIBCPP_INLINE_VISIBILITY
-    operator __cxx03_bool::__bool_type() const {
-        return !fail() ? &__cxx03_bool::__true_value : nullptr;
-    }
-#else
-    _LIBCPP_INLINE_VISIBILITY
-    _LIBCPP_EXPLICIT operator bool() const {return !fail();}
-#endif
+    explicit operator bool() const {return !fail();}
 
     _LIBCPP_INLINE_VISIBILITY bool operator!() const    {return  fail();}
     _LIBCPP_INLINE_VISIBILITY iostate rdstate() const   {return ios_base::rdstate();}
index 122ea37..7f827bb 100644 (file)
@@ -302,8 +302,7 @@ public:
 //    ~sentry() = default;
 
     _LIBCPP_INLINE_VISIBILITY
-        _LIBCPP_EXPLICIT
-        operator bool() const {return __ok_;}
+    explicit operator bool() const {return __ok_;}
 };
 
 template <class _CharT, class _Traits>
index 6a211f3..a8faa08 100644 (file)
@@ -254,8 +254,7 @@ public:
     ~sentry();
 
     _LIBCPP_INLINE_VISIBILITY
-        _LIBCPP_EXPLICIT
-        operator bool() const {return __ok_;}
+    explicit operator bool() const {return __ok_;}
 };
 
 template <class _CharT, class _Traits>
index b62e2a1..564f37a 100644 (file)
@@ -291,8 +291,7 @@ public:
     string message() const;
 
     _LIBCPP_INLINE_VISIBILITY
-        _LIBCPP_EXPLICIT
-        operator bool() const _NOEXCEPT {return __val_ != 0;}
+    explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
 };
 
 inline _LIBCPP_INLINE_VISIBILITY
@@ -368,8 +367,7 @@ public:
     string message() const;
 
     _LIBCPP_INLINE_VISIBILITY
-        _LIBCPP_EXPLICIT
-        operator bool() const _NOEXCEPT {return __val_ != 0;}
+    explicit operator bool() const _NOEXCEPT {return __val_ != 0;}
 };
 
 inline _LIBCPP_INLINE_VISIBILITY
diff --git a/libcxx/test/std/diagnostics/syserr/syserr.errcode/syserr.errcode.observers/bool.compile.fail.cpp b/libcxx/test/std/diagnostics/syserr/syserr.errcode/syserr.errcode.observers/bool.compile.fail.cpp
deleted file mode 100644 (file)
index 7a61c08..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// XFAIL: c++03
-
-// <system_error>
-
-// class error_code
-
-// explicit operator bool() const;
-
-#include <system_error>
-
-bool test_func(void)
-{
-    const std::error_code ec(0, std::generic_category());
-    return ec;   // conversion to bool is explicit; should fail.
-}
-
-int main(int, char**)
-{
-    return 0;
-}
index a67ddb3..2e14096 100644 (file)
 // explicit operator bool() const;
 
 #include <system_error>
-#include <string>
 #include <cassert>
+#include <string>
+#include <type_traits>
 
 #include "test_macros.h"
 
 int main(int, char**)
 {
+    static_assert(std::is_constructible<bool, std::error_code>::value, "");
+    static_assert(!std::is_convertible<std::error_code, bool>::value, "");
+
     {
         const std::error_code ec(6, std::generic_category());
         assert(static_cast<bool>(ec));
index 24fcbff..59896c8 100644 (file)
@@ -27,9 +27,7 @@ int main(int, char**)
     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), "");
-#if TEST_STD_VER >= 11
     static_assert((!std::is_convertible<std::ios, bool>::value), "");
-#endif
 
   return 0;
 }
index 5bd1190..0aded33 100644 (file)
@@ -14,6 +14,7 @@
 
 #include <exception>
 #include <cassert>
+#include <type_traits>
 
 #include "test_macros.h"
 
@@ -33,5 +34,5 @@ int main(int, char**)
     p3 = nullptr;
     assert(p3 == nullptr);
 
-  return 0;
+    return 0;
 }
index ac6499f..ecc47b8 100644 (file)
@@ -9,11 +9,6 @@
 // UNSUPPORTED: libcpp-has-no-threads
 // UNSUPPORTED: c++03, c++11
 
-// dylib support for shared_mutex was added in macosx10.12
-// XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.11
-// XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.10
-// XFAIL: use_system_cxx_lib && x86_64-apple-macosx10.9
-
 // <shared_mutex>
 
 // template <class Mutex> class shared_lock;
 
 #include "test_macros.h"
 
-std::shared_timed_mutex m;
+struct M {
+    void lock_shared() {}
+    void unlock_shared() {}
+};
 
 int main(int, char**)
 {
-    std::shared_lock<std::shared_timed_mutex> lk0;
+    static_assert(std::is_constructible<bool, std::shared_lock<M>>::value, "");
+    static_assert(!std::is_convertible<std::shared_lock<M>, bool>::value, "");
+
+    M m;
+    std::shared_lock<M> lk0;
     assert(static_cast<bool>(lk0) == false);
-    std::shared_lock<std::shared_timed_mutex> lk1(m);
+    std::shared_lock<M> lk1(m);
     assert(static_cast<bool>(lk1) == true);
     lk1.unlock();
     assert(static_cast<bool>(lk1) == false);
-    static_assert(noexcept(static_cast<bool>(lk0)), "explicit operator bool() must be noexcept");
+    ASSERT_NOEXCEPT(static_cast<bool>(lk0));
 
-  return 0;
+    return 0;
 }
index 681fc4b..5304f79 100644 (file)
 
 // template <class Mutex> class unique_lock;
 
-// explicit operator bool() const;
+// explicit operator bool() const noexcept;
 
 #include <mutex>
 #include <cassert>
+#include <type_traits>
 
 #include "test_macros.h"
 
@@ -23,12 +24,16 @@ std::mutex m;
 
 int main(int, char**)
 {
+    static_assert(std::is_constructible<bool, std::unique_lock<std::mutex> >::value, "");
+    static_assert(!std::is_convertible<std::unique_lock<std::mutex>, bool>::value, "");
+
     std::unique_lock<std::mutex> lk0;
     assert(static_cast<bool>(lk0) == false);
     std::unique_lock<std::mutex> lk1(m);
     assert(static_cast<bool>(lk1) == true);
     lk1.unlock();
     assert(static_cast<bool>(lk1) == false);
+    ASSERT_NOEXCEPT(static_cast<bool>(lk0));
 
   return 0;
 }
index fbfc658..165f1a9 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <functional>
 #include <cassert>
+#include <type_traits>
 
 #include "test_macros.h"
 
@@ -24,6 +25,9 @@ int g(int) {return 0;}
 
 int main(int, char**)
 {
+    static_assert(std::is_constructible<bool, std::function<void()> >::value, "");
+    static_assert(!std::is_convertible<std::function<void()>, bool>::value, "");
+
     {
     std::function<int(int)> f;
     assert(!f);
index bf6d495..d8c067c 100644 (file)
@@ -14,6 +14,7 @@
 
 #include <memory>
 #include <cassert>
+#include <type_traits>
 
 #include "test_macros.h"
 
@@ -25,6 +26,9 @@ struct B : A {};
 
 int main(int, char**)
 {
+    static_assert(std::is_constructible<bool, std::shared_ptr<A> >::value, "");
+    static_assert(!std::is_convertible<std::shared_ptr<A>, bool>::value, "");
+
     {
       const std::shared_ptr<int> p(new int(32));
       assert(p);