I have this header file and source file that are a part of a static library:
vector2.h
#pragma once
namespace lib2d
{
template<typename T>
struct Vector2
{
Vector2();
Vector2(const T& X, const T& Y);
T x;
T y;
};
}
template <typename T>
lib2d::Vector2<T>& operator +=(lib2d::Vector2<T>& left, const lib2d::Vector2<T>& right);
template < typename T>
lib2d::Vector2<T>& operator -=(lib2d::Vector2<T>& left, const lib2d::Vector2<T>& right);
vector2.cpp
#include "vector2.h"
namespace lib2d
{
template struct Vector2<float>;
template struct Vector2<int>;
template<typename T>
Vector2<T>::Vector2():
x{ 0 },
y{ 0 }
{
}
template<typename T>
Vector2<T>::Vector2(const T& X, const T& Y):
x{ X },
y{ Y }
{
}
}
template lib2d::Vector2<float>& operator +=(lib2d::Vector2<float>&, const lib2d::Vector2<float>&);
template lib2d::Vector2<int>& operator +=(lib2d::Vector2<int>&, const lib2d::Vector2<int>&);
template<typename T>
lib2d::Vector2<T>& operator +=(lib2d::Vector2<T>& left, const lib2d::Vector2<T>& right)
{
left.x += right.x;
left.y += right.y;
return left;
}
template lib2d::Vector2<float>& operator -=(lib2d::Vector2<float>&, const lib2d::Vector2<float>&);
template lib2d::Vector2<int>& operator -=(lib2d::Vector2<int>&, const lib2d::Vector2<int>&);
template<typename T>
lib2d::Vector2<T>& operator -=(lib2d::Vector2<T>& left, const lib2d::Vector2<T>& right)
{
left.x -= right.x;
left.y -= right.y;
return left;
}
When I build the library, everything builds successfully, but the vector2.cpp
file gives me compiler warnings.
The warnings are:
Warning C4667 'lib2d::Vector2<float> &operator +=(lib2d::Vector2<float> &,const lib2d::Vector2<float> &)': no function template defined that matches forced instantiation
Warning C4667 'lib2d::Vector2<int> &operator +=(lib2d::Vector2<int> &,const lib2d::Vector2<int> &)': no function template defined that matches forced instantiation
Warning C4667 'lib2d::Vector2<float> &operator -=(lib2d::Vector2<float> &,const lib2d::Vector2<float> &)': no function template defined that matches forced instantiation
Warning C4667 'lib2d::Vector2<int> &operator -=(lib2d::Vector2<int> &,const lib2d::Vector2<int> &)': no function template defined that matches forced instantiation
I have several other operator overloads in this file (that I didn't include here to shorten the post) that are giving me the same warnings.
I have used the finished library in another project and everything seems to be working correctly. I am tempted to just ignore the warnings and move on but I've been told that you should always listen to compiler warnings so I feel reluctant to do so.
What is causing these warnings? Clearly I've both declared and implemented the function templates so what does it mean when it says that there is no such function template defined?
I've tried googling for an answer but I can't find a single post talking about this particular warning.