Skip to main content
Code Review

Return to Question

Reworded to improve English grammar
Source Link
Toby Speight
  • 87.7k
  • 14
  • 104
  • 325

Continuing from theThis is a continuation of An Alternative Vector An Alternative Vector , taking a closer look at the copy assignment operator.

Taking a closer look at the copy assignment operator.
InIn the following code, I am only posting the new version of the copy assignment operatoroperator; all the rest of the code is unchanged.

Continuing from the An Alternative Vector

Taking a closer look at the copy assignment operator.
In the following code I am only posting the new version of the copy assignment operator all the rest of the code is unchanged.

This is a continuation of An Alternative Vector , taking a closer look at the copy assignment operator.

In the following code, I am only posting the new version of the copy assignment operator; all the rest of the code is unchanged.

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Continuing from the An Alternative Vector

Continuing from the An Alternative Vector

Tweeted twitter.com/StackCodeReview/status/703457013343330304
added 57 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
 template<typename T>
 class Vector
 {
 // The rest (see original question for details).
 friend class SimpleCopy<T>;
 friend class SimpleDestroy<T>;
 public:
 Vector& operator=(Vector const& value) noexcept(SimpleCopyableAssignableTraitNoThrow<T>::value)
 {
 SimpleCopy<T> copier;
 copier.simpleCopy(*this, value);
 return *this;
 }
 };
 template<typename T>
 class SimpleCopy<T, true>
 {
 public:
 // Neither constructor or destructor throw.
 // So we can optimize the copy.
 void simpleCopy(Vector<T>& dst, Vector<T> const& src) const
 {
 if (&dst == &src)
 {
 return;
 }
 // Destroy the members of the current object
 SimpleDestroy<T> destroyer;
 destroyer.destroyElements(dst);
 // Copy from the source object to the detonation
 // thus reusing the memory underneath. It only tries
 // to reallocate if the current object needs to be
 // re-sized.
 for(auto const& value: src)
 {
 dst.push(value);
 }
 }
 };
 template<typename T>
 class SimpleCopy<T, false>
 {
 public:
 // Constructor or Destructor may fail.
 // So use Copy & Swap idiom to handle resource
 // allocation correctly.
 void simpleCopy(Vector<T>& dst, Vector<T> const& src) const
 {
 Vector<T> tmp(src); // Copy
 srctmp.swap(dst); // Swap
 }
 }; 
}
 template<typename T>
 class Vector
 {
 // The rest (see original question for details).
 friend class SimpleCopy<T>;
 friend class SimpleDestroy<T>;
 public:
 Vector& operator=(Vector const& value)
 {
 SimpleCopy<T> copier;
 copier.simpleCopy(*this, value);
 return *this;
 }
 };
 template<typename T>
 class SimpleCopy<T, true>
 {
 public:
 // Neither constructor or destructor throw.
 // So we can optimize the copy.
 void simpleCopy(Vector<T>& dst, Vector<T> const& src) const
 {
 if (&dst == &src)
 {
 return;
 }
 // Destroy the members of the current object
 SimpleDestroy<T> destroyer;
 destroyer.destroyElements(dst);
 // Copy from the source object to the detonation
 // thus reusing the memory underneath. It only tries
 // to reallocate if the current object needs to be
 // re-sized.
 for(auto const& value: src)
 {
 dst.push(value);
 }
 }
 };
 template<typename T>
 class SimpleCopy<T, false>
 {
 public:
 // Constructor or Destructor may fail.
 // So use Copy & Swap idiom to handle resource
 // allocation correctly.
 void simpleCopy(Vector<T>& dst, Vector<T> const& src) const
 {
 Vector<T> tmp(src); // Copy
 src.swap(dst); // Swap
 }
 }; 
}
 template<typename T>
 class Vector
 {
 // The rest (see original question for details).
 friend class SimpleCopy<T>;
 friend class SimpleDestroy<T>;
 public:
 Vector& operator=(Vector const& value) noexcept(SimpleCopyableAssignableTraitNoThrow<T>::value)
 {
 SimpleCopy<T> copier;
 copier.simpleCopy(*this, value);
 return *this;
 }
 };
 template<typename T>
 class SimpleCopy<T, true>
 {
 public:
 // Neither constructor or destructor throw.
 // So we can optimize the copy.
 void simpleCopy(Vector<T>& dst, Vector<T> const& src) const
 {
 if (&dst == &src)
 {
 return;
 }
 // Destroy the members of the current object
 SimpleDestroy<T> destroyer;
 destroyer.destroyElements(dst);
 // Copy from the source object to the detonation
 // thus reusing the memory underneath. It only tries
 // to reallocate if the current object needs to be
 // re-sized.
 for(auto const& value: src)
 {
 dst.push(value);
 }
 }
 };
 template<typename T>
 class SimpleCopy<T, false>
 {
 public:
 // Constructor or Destructor may fail.
 // So use Copy & Swap idiom to handle resource
 // allocation correctly.
 void simpleCopy(Vector<T>& dst, Vector<T> const& src) const
 {
 Vector<T> tmp(src); // Copy
 tmp.swap(dst); // Swap
 }
 }; 
}
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /