I have a two different files with the same class name (here: A). Class is used in template specialization. Specialized template is used in static object initialization. I expect that compiler uses local class to solve template but as result shows it takes two times the same specialization.
Question is: How to prevent of creating such code (maybe some compiler setting to detect this) since it is really hard to debug?
------- template.h
template <typename T>
void Test() {
T* t = new T();
delete t;
}
------- classA1.cpp
#include <iostream>
#include "template.h"
class A {
public:
A() {
std::cout << "Hello I'm class A(1)" << std::endl;
}
};
struct Tester1 {
Tester1() {
Test<A>();
}
};
static Tester1 tester1;
------- classA2.cpp
#include <iostream>
#include "template.h"
class A {
public:
A() {
std::cout << "Hello I'm class A(2)" << std::endl;
}
};
struct Tester2 {
Tester2() {
Test<A>();
}
};
static Tester2 tester2;
------- main.cpp
#include <iostream>
int main() {
std::cout << "Hello from main!" << std::endl;
return 0;
};
Result:
Hello I'm class A(1)
Hello I'm class A(1)
Hello from main!