I was working with a template class that contains a static variable. The structure of code is as follows.
Header.h
template<class T> class Foo
{
public:
static int count;
Foo() {
count++;
}
void printCount() {
cout << count << endl;
}
};
template<class T> int Foo<T>::count;
Source.cpp
#include "Header.h"
template<> int Foo<int>::count = 5;
main.cpp
#include <iostream>
using namespace std;
#include "Header.h"
int main()
{
Foo<int> obj1;
Foo<int> obj2;
obj1.printCount();
obj2.printCount();
return 0;
}
the output on xcode8.3.3 is:
7
7
whereas the output on Visual Studio 2015 is:
2
2
i.e. the specific instantiation overrides the generic instantiation in xcode8.3.3 but not in Visual Studio 2015. Could someone explain this difference in behavior? Thanks in advance.