浏览代码

Adding noexcept declarations

Samuel Jaffe 8 年之前
父节点
当前提交
2bf132fb86
共有 6 个文件被更改,包括 89 次插入77 次删除
  1. 17 17
      const_propogating_ptr.hpp
  2. 11 7
      const_ptr.hpp
  3. 16 10
      copy_ptr.hpp
  4. 17 15
      maybe_null.hpp
  5. 10 10
      not_null.hpp
  6. 18 18
      ptr_compare.hpp

+ 17 - 17
const_propogating_ptr.hpp

@@ -20,41 +20,41 @@ public:
   using const_pointer = element_type const *;
   using const_reference = element_type const &;
 
-  const_propogating_ptr() : _ptr(nullptr) {}
-  const_propogating_ptr(P const & p) : _ptr(p) {}
-  const_propogating_ptr(P && p) : _ptr(std::move(p)) {}
+  const_propogating_ptr() noexcept : _ptr(nullptr) {}
+  const_propogating_ptr(P const & p) noexcept(std::is_nothrow_copy_constructible<P>::value) : _ptr(p) {}
+  const_propogating_ptr(P && p) noexcept(std::is_nothrow_move_constructible<P>::value) : _ptr(std::move(p)) {}
   
-  const_propogating_ptr(const_propogating_ptr & other) : _ptr(other._ptr) {}
-  const_propogating_ptr(const_propogating_ptr &&) = default;
+  const_propogating_ptr(const_propogating_ptr &) noexcept(std::is_nothrow_copy_constructible<P>::value) = default;
+  const_propogating_ptr(const_propogating_ptr &&) noexcept(std::is_nothrow_move_constructible<P>::value) = default;
   const_propogating_ptr(const_propogating_ptr const &) = delete;
-  const_propogating_ptr& operator=(const_propogating_ptr & other) { _ptr = other._ptr; return *this; }
-  const_propogating_ptr& operator=(const_propogating_ptr &&) = default;
+  const_propogating_ptr& operator=(const_propogating_ptr &) noexcept(std::is_nothrow_copy_constructible<P>::value) = default;
+  const_propogating_ptr& operator=(const_propogating_ptr &&) noexcept(std::is_nothrow_move_constructible<P>::value) = default;
   const_propogating_ptr& operator=(const_propogating_ptr const &) = delete;
   
   template <typename Y>
-  explicit operator const_propogating_ptr<Y>() & {
+  explicit operator const_propogating_ptr<Y>() & noexcept(std::is_nothrow_copy_constructible<Y>::value) {
     return _ptr;
   }
   
   template <typename Y>
-  explicit operator const_ptr<Y>() const {
+  explicit operator const_ptr<Y>() const noexcept(std::is_nothrow_copy_constructible<Y>::value) {
     return _ptr;
   }
   
   template <typename Y>
   explicit operator const_propogating_ptr<Y>() const & = delete;
   
-  operator bool() const {
-    return bool(_ptr);
+  operator bool() const noexcept {
+    return static_cast<bool>(_ptr);
   }
   
-  reference operator*() { return *_ptr; }
-  pointer get() { return std::addressof(operator*()); }
-  pointer operator->() { return get(); }
+  reference operator*() noexcept { return *_ptr; }
+  pointer get() noexcept { return std::addressof(operator*()); }
+  pointer operator->() noexcept { return get(); }
   
-  const_reference operator*() const { return *_ptr; }
-  const_pointer get() const { return std::addressof(operator*()); }
-  const_pointer operator->() const { return get(); }
+  const_reference operator*() const noexcept { return *_ptr; }
+  const_pointer get() const noexcept { return std::addressof(operator*()); }
+  const_pointer operator->() const noexcept { return get(); }
   
 private:
   P _ptr;

+ 11 - 7
const_ptr.hpp

@@ -18,18 +18,22 @@ public:
   using pointer = element_type const *;
   using reference = element_type const &;
 
-  const_ptr() : _ptr(nullptr) {}
-  const_ptr(P const & p) : _ptr(p) {}
-  const_ptr(P && p) : _ptr(std::move(p)) {}
+  const_ptr() noexcept : _ptr(nullptr) {}
+  const_ptr(P const & p) noexcept(std::is_nothrow_copy_constructible<P>::value) : _ptr(p) {}
+  const_ptr(P && p) noexcept(std::is_nothrow_move_constructible<P>::value) : _ptr(std::move(p)) {}
   
   template <typename Y>
-  explicit operator const_ptr<Y>() const {
+  explicit operator const_ptr<Y>() const noexcept(std::is_nothrow_copy_constructible<Y>::value) {
     return _ptr;
   }
+  
+  operator bool() const noexcept {
+    return static_cast<bool>(_ptr);
+  }
 
-  reference operator*() const { return *_ptr; }
-  pointer get() const { return std::addressof(operator*()); }
-  pointer operator->() const { return get(); }
+  reference operator*() const noexcept { return *_ptr; }
+  pointer get() const noexcept { return std::addressof(operator*()); }
+  pointer operator->() const noexcept { return get(); }
 private:
   P _ptr;
 };

+ 16 - 10
copy_ptr.hpp

@@ -34,11 +34,14 @@ public:
   using pointer = element_type *;
   using reference = element_type &;
   
-  copy_ptr() : _ptr(nullptr) {}
+  copy_ptr() noexcept : _ptr(nullptr) {}
   copy_ptr(T * const & p) = delete;
-  copy_ptr(T * && p) : _ptr(std::move(p)) {}
+  copy_ptr(T * && p) noexcept(std::is_nothrow_move_constructible<T>::value) : _ptr(std::move(p)) {}
   copy_ptr(copy_ptr const & other) : _ptr(Copy(other._ptr)) {}
-  copy_ptr(copy_ptr &&) = default;
+  copy_ptr(copy_ptr && other) noexcept(noexcept(swap(_ptr, other._ptr))) : _ptr(nullptr) { swap(_ptr, other._ptr); }
+  static copy_ptr copy_of(T * const & p) {
+    return Copy(p);
+  }
   
   template <typename Y>
   explicit operator copy_ptr<Y>() const {
@@ -47,19 +50,22 @@ public:
   
   ~copy_ptr() { delete _ptr; }
   
-  copy_ptr & operator=(copy_ptr const & other) {
-    swap(copy_ptr{other});
+  copy_ptr & operator=(copy_ptr const & other) noexcept(noexcept(swap(_ptr, other._ptr))) {
+    swap(_ptr, copy_ptr{other}._ptr);
+    return *this;
+  }
+  copy_ptr & operator=(copy_ptr && other) noexcept(noexcept(swap(_ptr, other._ptr))) {
+    swap(_ptr, other._ptr);
     return *this;
   }
-  copy_ptr & operator=(copy_ptr &&) = default;
   
-  operator bool() const {
+  operator bool() const noexcept {
     return static_cast<bool>(_ptr);
   }
   
-  pointer get() const { return _ptr; }
-  pointer operator->() const { return get(); }
-  reference operator*() const { return *get(); }
+  pointer get() const noexcept { return _ptr; }
+  pointer operator->() const noexcept { return get(); }
+  reference operator*() const noexcept { return *get(); }
 private:
   T * _ptr;
 };

+ 17 - 15
maybe_null.hpp

@@ -27,18 +27,18 @@ public:
   using pointer = element_type *;
   using reference = element_type &;
   
-  maybe_null() : _ptr(nullptr) {}
-  maybe_null(T const & p) : _ptr(p) { }
-  maybe_null(T && p) : _ptr(std::move(p)) { }
-  maybe_null(maybe_null const&) = default;
+  maybe_null() noexcept : _ptr(nullptr) {}
+  maybe_null(T const & p) noexcept(std::is_nothrow_copy_constructible<T>::value) : _ptr(p) { }
+  maybe_null(T && p) noexcept(std::is_nothrow_move_constructible<T>::value) : _ptr(std::move(p)) { }
+  maybe_null(maybe_null const&) noexcept(std::is_nothrow_copy_constructible<T>::value) = default;
   
   template <typename Y>
-  explicit operator maybe_null<Y>() const {
+  explicit operator maybe_null<Y>() const noexcept(std::is_nothrow_copy_constructible<Y>::value) {
     return _ptr;
   }
   
-  maybe_null& operator=(maybe_null const&) = default;
-  template <typename Y> maybe_null& operator=(maybe_null<Y> const&other) {
+  maybe_null& operator=(maybe_null const&) noexcept = default;
+  template <typename Y> maybe_null& operator=(maybe_null<Y> const&other) noexcept {
     if (_ptr != other._ptr) {
       _ptr = other._ptr;
 #if defined( DEBUG )
@@ -48,25 +48,27 @@ public:
     return *this;
   }
   
-  operator bool() const {
+  operator bool() const noexcept {
 #if defined( DEBUG )
     tested_ = true;
 #endif
     return static_cast<bool>(_ptr);
   }
   
-  pointer get() const { return std::addressof(*_ptr); }
-  pointer operator->() const {
+  pointer get() const noexcept { return std::addressof(*_ptr); }
+  pointer operator->() const /*throw(unchecked_pointer_exception)*/ {
     return std::addressof(operator*());
   }
-  reference operator*() const {
 #if defined( DEBUG )
-    if ( !tested_ ) {
-      throw unchecked_pointer_exception{"did not verify that pointer was non-null"};
-    }
-#endif
+  reference operator*() const /*throw(unchecked_pointer_exception)*/ {
+    if ( !tested_ ) { throw unchecked_pointer_exception{"did not verify that pointer was non-null"}; }
     return *_ptr;
   }
+#else
+  reference operator*() const noexcept {
+    return *_ptr;
+  }
+#endif
 private:
   T _ptr;
 #if defined( DEBUG )

+ 10 - 10
not_null.hpp

@@ -32,29 +32,29 @@ public:
   explicit not_null(int) = delete;
   not_null(T const & p) : _ptr(p) { validate(); }
   not_null(T && p) : _ptr(std::move(p)) { validate(); }
-  not_null(not_null const&) = default;
+  not_null(not_null const&) noexcept(std::is_nothrow_copy_constructible<Y>::value) = default;
 
   template <typename Y>
-  explicit operator maybe_null<Y>() const {
+  explicit operator maybe_null<Y>() const noexcept(std::is_nothrow_copy_constructible<Y>::value) {
     return _ptr;
   }
   template <typename Y>
-  explicit operator not_null<Y>() const {
+  explicit operator not_null<Y>() const noexcept(std::is_nothrow_copy_constructible<Y>::value) {
     return _ptr;
   }
 
-  not_null& operator=(not_null const&) = default;
-  template <typename Y> not_null& operator=(not_null<Y> const&other) {
+  not_null& operator=(not_null const&) noexcept(std::is_nothrow_copy_constructible<Y>::value) = default;
+  template <typename Y> not_null& operator=(not_null<Y> const&other) noexcept(std::is_nothrow_copy_constructible<T>::value) {
     _ptr = other._ptr;
     return *this;
   }
   
-  explicit operator maybe_null<T>() const;
-  operator bool() const { return true; }
+  explicit operator maybe_null<T>() const noexcept(std::is_nothrow_copy_constructible<T>::value);
+  operator bool() const noexcept { return true; }
   
-  pointer get() const { return std::addressof(operator*()); }
-  reference operator*() const { return *_ptr; }
-  pointer operator->() const { return get(); }
+  pointer get() const noexcept { return std::addressof(operator*()); }
+  reference operator*() const noexcept { return *_ptr; }
+  pointer operator->() const noexcept { return get(); }
   
 private:
   void validate() {

+ 18 - 18
ptr_compare.hpp

@@ -14,17 +14,17 @@
 
 #define POINTER_TEMPLATE_COMPARE_FULL( ptr_t, T_tname, T, U_tname, U ) \
 template <EXPAND T_tname, EXPAND U_tname> \
-bool operator==(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+bool operator==(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) noexcept { \
   return lhs.get() == rhs.get(); \
 } \
 \
 template <EXPAND T_tname, EXPAND U_tname> \
-bool operator!=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+bool operator!=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) noexcept { \
   return !(lhs == rhs); \
 } \
 \
 template <EXPAND T_tname, EXPAND U_tname> \
-bool operator< (ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+bool operator< (ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) noexcept { \
   typedef typename std::common_type< \
   typename ptr_t< EXPAND T >::pointer, \
   typename ptr_t< EXPAND U >::pointer>::type V; \
@@ -32,78 +32,78 @@ bool operator< (ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
 } \
 \
 template <EXPAND T_tname, EXPAND U_tname> \
-bool operator> (ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+bool operator> (ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) noexcept { \
   return rhs < lhs; \
 } \
 \
 template <EXPAND T_tname, EXPAND U_tname> \
-bool operator<=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+bool operator<=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) noexcept { \
   return !(rhs < lhs); \
 } \
 \
 template <EXPAND T_tname, EXPAND U_tname> \
-bool operator>=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) { \
+bool operator>=(ptr_t< EXPAND T > const&lhs, ptr_t< EXPAND U > const&rhs) noexcept { \
   return !(lhs < rhs); \
 } \
 \
 template <EXPAND T_tname> \
-bool operator==(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+bool operator==(ptr_t< EXPAND T > const&lhs, std::nullptr_t) noexcept { \
   return !lhs; \
 } \
 \
 template <EXPAND T_tname> \
-bool operator==(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+bool operator==(std::nullptr_t, ptr_t< EXPAND T > const&rhs) noexcept { \
   return !rhs; \
 } \
 \
 template <EXPAND T_tname> \
-bool operator!=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+bool operator!=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) noexcept { \
   return static_cast<bool>(lhs); \
 } \
 \
 template <EXPAND T_tname> \
-bool operator!=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+bool operator!=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) noexcept { \
   return static_cast<bool>(rhs); \
 } \
 \
 template <EXPAND T_tname> \
-bool operator< (ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+bool operator< (ptr_t< EXPAND T > const&lhs, std::nullptr_t) noexcept { \
   typedef typename ptr_t< EXPAND T >::pointer V; \
   return std::less<V>(lhs.get(), nullptr); \
 } \
 \
 template <EXPAND T_tname> \
-bool operator< (std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+bool operator< (std::nullptr_t, ptr_t< EXPAND T > const&rhs) noexcept { \
   typedef typename ptr_t< EXPAND T >::pointer V; \
   return std::less<V>(nullptr, rhs.get()); \
 } \
 \
 template <EXPAND T_tname> \
-bool operator> (ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+bool operator> (ptr_t< EXPAND T > const&lhs, std::nullptr_t) noexcept { \
   return nullptr < lhs; \
 } \
 \
 template <EXPAND T_tname> \
-bool operator> (std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+bool operator> (std::nullptr_t, ptr_t< EXPAND T > const&rhs) noexcept { \
   return rhs < nullptr; \
 } \
 \
 template <EXPAND T_tname> \
-bool operator<=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+bool operator<=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) noexcept { \
   return !(nullptr < lhs); \
 } \
 \
 template <EXPAND T_tname> \
-bool operator<=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+bool operator<=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) noexcept { \
   return !(rhs < nullptr); \
 } \
 \
 template <EXPAND T_tname> \
-bool operator>=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) { \
+bool operator>=(ptr_t< EXPAND T > const&lhs, std::nullptr_t) noexcept { \
   return !(lhs < nullptr); \
 } \
 \
 template <EXPAND T_tname> \
-bool operator>=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) { \
+bool operator>=(std::nullptr_t, ptr_t< EXPAND T > const&rhs) noexcept { \
   return !(nullptr < rhs); \
 }