So I have a class template for example in
Template.h
template <typename T>
class Something {
public:
static_assert(std::is_floating_point<T>::value, "Floating Point only");
};
and I separated the float
and double
specialization into different .h files
float.h
#include "Template.h"
template<>
class Something<float> {
public:
std::string Name = "Float";
void DoSomething() {
std::cout << "Float Thing\n";
}
};
and
Double.h
#include "Template.h"
template<>
class Something<float> {
public:
std::string Name = "Double";
void DoSomething() {
std::cout << "Double Thing\n";
}
};
I include the three files in order in my .cpp
file and it works fine
but I need to add another function to the float
specialization which takes Something as a parameter
void SayDouble(Something<double> _D) {
std::cout << _D.Name << '\n';
}
and then include Double.h
after template.h
and it works but when I try to do the same in Double.h
and add a function which takes or returns Something<float>
and includes float.h
after tempalte.h
.
it gives me a lot of errors about re-instantiation
Float.h(17,19): error C2039: 'Name': is not a member of 'Something<double>'
1>Float.h(16): message : see declaration of 'Something<double>'
1>Double.h(7,1): error C2908: explicit specialization; 'Something<double>' has already been instantiated
1>Double.h(19,2): error C2766: explicit specialization; 'Something<double>' has already been defined
1>Float.h(16): message : see previous definition of 'Something<double>'
1>Float.h(19,2): error C2766: explicit specialization; 'Something<float>' has already been defined
1>Float.h(7): message : see previous definition of 'Something<float>
I don't really understand the problem what I guessed is that maybe having an specialization of the template in the functions signature instantiates the template then when it tries to instantiate it again in the definition it gives "has already been defined error" But I don't know how to rly solve this as I need to have functions referring to other specializations
I am using Visual Studio Community 2019 version 16.11.6