[libcxx-commits] [libcxx] f86c2b6 - [libc++] Add `explicit` to a bunch of internal detail ctors.

Arthur O'Dwyer via libcxx-commits libcxx-commits at lists.llvm.org
Tue Mar 1 11:37:16 PST 2022


Author: Arthur O'Dwyer
Date: 2022-03-01T14:37:06-05:00
New Revision: f86c2b6f1f3682cef24fb74edd5d02eb8f2707f7

URL: https://github.com/llvm/llvm-project/commit/f86c2b6f1f3682cef24fb74edd5d02eb8f2707f7
DIFF: https://github.com/llvm/llvm-project/commit/f86c2b6f1f3682cef24fb74edd5d02eb8f2707f7.diff

LOG: [libc++] Add `explicit` to a bunch of internal detail ctors.

Notably the following ctors remain non-explicit because they
are used as implicit conversions in too many places:
* __debug_less(_Compare&)
* __map_iterator(_TreeIterator)
* __map_const_iterator(_TreeIterator)
* __hash_map_iterator(_HashIterator)
* __hash_map_const_iterator(_HashIterator)

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

Added: 
    

Modified: 
    libcxx/include/__bit_reference
    libcxx/include/__debug
    libcxx/include/__hash_table
    libcxx/include/__iterator/common_iterator.h
    libcxx/include/__iterator/istreambuf_iterator.h
    libcxx/include/__memory/compressed_pair.h
    libcxx/include/__string
    libcxx/include/deque
    libcxx/include/iomanip

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference
index 423acee4aa7c..53af4b3b48b7 100644
--- a/libcxx/include/__bit_reference
+++ b/libcxx/include/__bit_reference
@@ -88,7 +88,7 @@ public:
         {return __bit_iterator<_Cp, false>(__seg_, static_cast<unsigned>(__libcpp_ctz(__mask_)));}
 private:
     _LIBCPP_INLINE_VISIBILITY
-    __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
+    explicit __bit_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
         : __seg_(__s), __mask_(__m) {}
 };
 
@@ -164,7 +164,7 @@ public:
 private:
     _LIBCPP_INLINE_VISIBILITY
     _LIBCPP_CONSTEXPR
-    __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
+    explicit __bit_const_reference(__storage_pointer __s, __storage_type __m) _NOEXCEPT
         : __seg_(__s), __mask_(__m) {}
 
     __bit_const_reference& operator=(const __bit_const_reference&) = delete;
@@ -1250,7 +1250,7 @@ public:
 
 private:
     _LIBCPP_INLINE_VISIBILITY
-    __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
+    explicit __bit_iterator(__storage_pointer __s, unsigned __ctz) _NOEXCEPT
         : __seg_(__s), __ctz_(__ctz) {}
 
     friend typename _Cp::__self;

diff  --git a/libcxx/include/__debug b/libcxx/include/__debug
index b7677a22c859..a4160b3b7f4a 100644
--- a/libcxx/include/__debug
+++ b/libcxx/include/__debug
@@ -66,7 +66,7 @@ struct _LIBCPP_TYPE_VIS __c_node
     __c_node& operator=(const __c_node&) = delete;
 
     _LIBCPP_INLINE_VISIBILITY
-    __c_node(void* __c, __c_node* __next)
+    explicit __c_node(void* __c, __c_node* __next)
         : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {}
     virtual ~__c_node();
 
@@ -83,7 +83,7 @@ template <class _Cont>
 struct _C_node
     : public __c_node
 {
-    _C_node(void* __c, __c_node* __n)
+    explicit _C_node(void* __c, __c_node* __n)
         : __c_node(__c, __n) {}
 
     virtual bool __dereferenceable(const void*) const;
@@ -141,7 +141,7 @@ class _LIBCPP_TYPE_VIS __libcpp_db
     __i_node** __iend_;
     size_t   __isz_;
 
-    __libcpp_db();
+    explicit __libcpp_db();
 public:
     __libcpp_db(const __libcpp_db&) = delete;
     __libcpp_db& operator=(const __libcpp_db&) = delete;

diff  --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table
index 36f2ef7a2c7a..5de00a3abcb5 100644
--- a/libcxx/include/__hash_table
+++ b/libcxx/include/__hash_table
@@ -362,14 +362,14 @@ public:
 private:
 #if _LIBCPP_DEBUG_LEVEL == 2
     _LIBCPP_INLINE_VISIBILITY
-    __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
+    explicit __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
         : __node_(__node)
         {
             __get_db()->__insert_ic(this, __c);
         }
 #else
     _LIBCPP_INLINE_VISIBILITY
-    __hash_iterator(__next_pointer __node) _NOEXCEPT
+    explicit __hash_iterator(__next_pointer __node) _NOEXCEPT
         : __node_(__node)
         {}
 #endif
@@ -480,14 +480,14 @@ public:
 private:
 #if _LIBCPP_DEBUG_LEVEL == 2
     _LIBCPP_INLINE_VISIBILITY
-    __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
+    explicit __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
         : __node_(__node)
         {
             __get_db()->__insert_ic(this, __c);
         }
 #else
     _LIBCPP_INLINE_VISIBILITY
-    __hash_const_iterator(__next_pointer __node) _NOEXCEPT
+    explicit __hash_const_iterator(__next_pointer __node) _NOEXCEPT
         : __node_(__node)
         {}
 #endif
@@ -593,8 +593,8 @@ public:
 private:
 #if _LIBCPP_DEBUG_LEVEL == 2
     _LIBCPP_INLINE_VISIBILITY
-    __hash_local_iterator(__next_pointer __node, size_t __bucket,
-                          size_t __bucket_count, const void* __c) _NOEXCEPT
+    explicit __hash_local_iterator(__next_pointer __node, size_t __bucket,
+                                   size_t __bucket_count, const void* __c) _NOEXCEPT
         : __node_(__node),
           __bucket_(__bucket),
           __bucket_count_(__bucket_count)
@@ -605,8 +605,8 @@ private:
         }
 #else
     _LIBCPP_INLINE_VISIBILITY
-    __hash_local_iterator(__next_pointer __node, size_t __bucket,
-                          size_t __bucket_count) _NOEXCEPT
+    explicit __hash_local_iterator(__next_pointer __node, size_t __bucket,
+                                   size_t __bucket_count) _NOEXCEPT
         : __node_(__node),
           __bucket_(__bucket),
           __bucket_count_(__bucket_count)
@@ -736,8 +736,8 @@ public:
 private:
 #if _LIBCPP_DEBUG_LEVEL == 2
     _LIBCPP_INLINE_VISIBILITY
-    __hash_const_local_iterator(__next_pointer __node_ptr, size_t __bucket,
-                                size_t __bucket_count, const void* __c) _NOEXCEPT
+    explicit __hash_const_local_iterator(__next_pointer __node_ptr, size_t __bucket,
+                                         size_t __bucket_count, const void* __c) _NOEXCEPT
         : __node_(__node_ptr),
           __bucket_(__bucket),
           __bucket_count_(__bucket_count)
@@ -748,8 +748,8 @@ private:
         }
 #else
     _LIBCPP_INLINE_VISIBILITY
-    __hash_const_local_iterator(__next_pointer __node_ptr, size_t __bucket,
-                                size_t __bucket_count) _NOEXCEPT
+    explicit __hash_const_local_iterator(__next_pointer __node_ptr, size_t __bucket,
+                                         size_t __bucket_count) _NOEXCEPT
         : __node_(__node_ptr),
           __bucket_(__bucket),
           __bucket_count_(__bucket_count)

diff  --git a/libcxx/include/__iterator/common_iterator.h b/libcxx/include/__iterator/common_iterator.h
index 505e4f1f3f0d..abbd8f4038fb 100644
--- a/libcxx/include/__iterator/common_iterator.h
+++ b/libcxx/include/__iterator/common_iterator.h
@@ -42,7 +42,7 @@ class common_iterator {
 
     iter_value_t<_Iter> __value;
     // We can move __x because the only caller verifies that __x is not a reference.
-    constexpr __proxy(iter_reference_t<_Iter>&& __x)
+    constexpr explicit __proxy(iter_reference_t<_Iter>&& __x)
       : __value(_VSTD::move(__x)) {}
 
   public:
@@ -55,7 +55,7 @@ class common_iterator {
     friend common_iterator;
 
     iter_value_t<_Iter> __value;
-    constexpr __postfix_proxy(iter_reference_t<_Iter>&& __x)
+    constexpr explicit __postfix_proxy(iter_reference_t<_Iter>&& __x)
       : __value(_VSTD::forward<iter_reference_t<_Iter>>(__x)) {}
 
   public:

diff  --git a/libcxx/include/__iterator/istreambuf_iterator.h b/libcxx/include/__iterator/istreambuf_iterator.h
index 3b16f7947678..bc53a6a1c80e 100644
--- a/libcxx/include/__iterator/istreambuf_iterator.h
+++ b/libcxx/include/__iterator/istreambuf_iterator.h
@@ -50,7 +50,8 @@ _LIBCPP_SUPPRESS_DEPRECATED_POP
     {
         char_type __keep_;
         streambuf_type* __sbuf_;
-        _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s)
+        _LIBCPP_INLINE_VISIBILITY
+        explicit __proxy(char_type __c, streambuf_type* __s)
             : __keep_(__c), __sbuf_(__s) {}
         friend class istreambuf_iterator;
     public:

diff  --git a/libcxx/include/__memory/compressed_pair.h b/libcxx/include/__memory/compressed_pair.h
index 852031f17d2f..89e5820e992a 100644
--- a/libcxx/include/__memory/compressed_pair.h
+++ b/libcxx/include/__memory/compressed_pair.h
@@ -32,17 +32,17 @@ struct __compressed_pair_elem {
   using reference = _Tp&;
   using const_reference = const _Tp&;
 
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __compressed_pair_elem(__default_init_tag) {}
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __compressed_pair_elem(__value_init_tag) : __value_() {}
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_() {}
 
   template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, typename decay<_Up>::type>::value> >
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit
-  __compressed_pair_elem(_Up&& __u) : __value_(std::forward<_Up>(__u)) {}
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
+  explicit __compressed_pair_elem(_Up&& __u) : __value_(std::forward<_Up>(__u)) {}
 
 #ifndef _LIBCPP_CXX03_LANG
   template <class... _Args, size_t... _Indices>
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
-  __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
+  explicit __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args, __tuple_indices<_Indices...>)
       : __value_(std::forward<_Args>(std::get<_Indices>(__args))...) {}
 #endif
 
@@ -60,9 +60,9 @@ struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
   using const_reference = const _Tp&;
   using __value_type = _Tp;
 
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __compressed_pair_elem() = default;
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __compressed_pair_elem(__default_init_tag) {}
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __compressed_pair_elem(__value_init_tag) : __value_type() {}
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem() = default;
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__default_init_tag) {}
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __compressed_pair_elem(__value_init_tag) : __value_type() {}
 
   template <class _Up, class = __enable_if_t<!is_same<__compressed_pair_elem, typename decay<_Up>::type>::value> >
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
@@ -101,17 +101,17 @@ class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
     >
   >
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
-  __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
+  explicit __compressed_pair() : _Base1(__value_init_tag()), _Base2(__value_init_tag()) {}
 
   template <class _U1, class _U2>
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
-  __compressed_pair(_U1&& __t1, _U2&& __t2) : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
+  explicit __compressed_pair(_U1&& __t1, _U2&& __t2) : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
 
 #ifndef _LIBCPP_CXX03_LANG
   template <class... _Args1, class... _Args2>
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX14
-  __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
-                    tuple<_Args2...> __second_args)
+  explicit __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
+                             tuple<_Args2...> __second_args)
       : _Base1(__pc, std::move(__first_args), typename __make_tuple_indices<sizeof...(_Args1)>::type()),
         _Base2(__pc, std::move(__second_args), typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
 #endif

diff  --git a/libcxx/include/__string b/libcxx/include/__string
index 479893bbde90..bde5e5c1907c 100644
--- a/libcxx/include/__string
+++ b/libcxx/include/__string
@@ -1163,8 +1163,8 @@ struct __quoted_output_proxy
     _CharT  __delim;
     _CharT  __escape;
 
-    __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
-    : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
+    explicit __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
+        : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
     //  This would be a nice place for a string_ref
 };
 

diff  --git a/libcxx/include/deque b/libcxx/include/deque
index b0fd1487f379..a6f2a676f3b4 100644
--- a/libcxx/include/deque
+++ b/libcxx/include/deque
@@ -450,7 +450,7 @@ public:
         {return !(__x < __y);}
 
 private:
-    _LIBCPP_INLINE_VISIBILITY __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
+    _LIBCPP_INLINE_VISIBILITY explicit __deque_iterator(__map_iterator __m, pointer __p) _NOEXCEPT
         : __m_iter_(__m), __ptr_(__p) {}
 
     template <class _Tp, class _Ap> friend class __deque_base;

diff  --git a/libcxx/include/iomanip b/libcxx/include/iomanip
index 3d7915410fd0..05826cd4e31b 100644
--- a/libcxx/include/iomanip
+++ b/libcxx/include/iomanip
@@ -573,7 +573,7 @@ basic_ostream<_CharT, _Traits>& operator<<(
          basic_ostream<_CharT, _Traits>& __os,
          const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
 {
-    return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
+    return __quoted_output(__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
 }
 
 template <class _CharT, class _Traits, class _Allocator>
@@ -583,8 +583,8 @@ struct __quoted_proxy
     _CharT  __delim;
     _CharT  __escape;
 
-    __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
-    : __string(__s), __delim(__d), __escape(__e) {}
+    explicit __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
+        : __string(__s), __delim(__d), __escape(__e) {}
 };
 
 template <class _CharT, class _Traits, class _Allocator>


        


More information about the libcxx-commits mailing list