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.
Continuing from the An Alternative Vector An Alternative Vector
Continuing from the An Alternative Vector
Continuing from the An Alternative Vector
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
}
};
}