Specified copy constructor for Vec2 and Vec3 to change the x,y and z references and not keep references to the coordinates of the original object.
Fixed the SFINAE not triggering on normalize() because there was no template substitution.
This commit is contained in:
parent
4da6167836
commit
8cdcc9750b
1 changed files with 18 additions and 6 deletions
|
@ -29,10 +29,12 @@ struct CoordinatesVector
|
|||
return sqrt(sum_of_squares);
|
||||
}
|
||||
|
||||
// Use SFINAE to allow normalization of floating point
|
||||
// Use SFINAE to allow normalization of floating point.
|
||||
// The template argument is used to force substitution and thus checking the enable_if.
|
||||
/// Normalizes the vector using its euclidean norm.
|
||||
/// \return Nothing, the vector is normalized in-place.
|
||||
typename std::enable_if<std::is_floating_point<T>::value,void>::type
|
||||
template <typename U = T>
|
||||
typename std::enable_if<std::is_floating_point<U>::value,void>::type
|
||||
normalize()
|
||||
{
|
||||
double norm = magnitude();
|
||||
|
@ -104,6 +106,11 @@ struct Vec2 : public CoordinatesVector<T,2>
|
|||
|
||||
Vec2() = default;
|
||||
|
||||
Vec2(const Vec2<T>& original): x(CoordinatesVector<T,2>::coordinates[0]), y(CoordinatesVector<T,2>::coordinates[1])
|
||||
{
|
||||
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,2>::coordinates));
|
||||
}
|
||||
|
||||
// Cast-copy constructor in order to use the operators of the mother class.
|
||||
Vec2(const CoordinatesVector<T,2>& origin)
|
||||
{
|
||||
|
@ -133,12 +140,17 @@ struct Vec3 : CoordinatesVector<T,3>
|
|||
|
||||
Vec3() = default;
|
||||
|
||||
Vec3(const Vec3<T>& original): x(CoordinatesVector<T,3>::coordinates[0]), y(CoordinatesVector<T,3>::coordinates[1]), z(CoordinatesVector<T,3>::coordinates[2])
|
||||
{
|
||||
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,3>::coordinates));
|
||||
}
|
||||
|
||||
// Cast-copy constructor in order to use the operators of the mother class.
|
||||
Vec3(const CoordinatesVector<T,3>& origin)
|
||||
Vec3(const CoordinatesVector<T,3>& original)
|
||||
{
|
||||
if (this != &origin)
|
||||
if (this != &original)
|
||||
{
|
||||
std::copy(std::begin(origin.coordinates),std::end(origin.coordinates),std::begin(CoordinatesVector<T,2>::coordinates));
|
||||
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,2>::coordinates));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue