
Static members in Templates
Quote:
> template <class T>
> class Test
> {
> public:
> static T Value;
> };
template<class T>
T Test<T>::Value;
Quote:
> int main()
> {
> Test<int>::Value=2;
> Test<int> Blah;
> Blah.Value=1;
> return 0;
> }
> -----8<-----8<-----8<-----8<-----8<-----
> At compiling time, I get an C4101 'Blah' : unreferenced local variable
> At linktime I get an LNK2001: unresolved external symbol "public: static int
> Test<int>::Value"
> I look up in the MSDN and found the Liner error bug acknowledged in Q179273.
> But I am not satisfied with the recommended "resolution". I need en explicit
> global variable for every template-instance and I dont know a workaround
> except of manuall creation.
> Could someone help me?
I just compiled, linked, and ran your program with the addition shown
above, which is not, I believe, a workaround, but rather the correct
way to do it. I still got the unreferenced local variable warning,
which makes sense, because Blah really isn't being referenced. The
knowledge base article you cite is just pointing out, if you'd wanted
to initialize Value with some value other than 0, saying
template<class T>
T Test<T>::Value(42);
would not work (this is a bug), but that
template<class T>
T Test<T>::Value = 42;
does work.
I'm using VC6 sp3. I wouldn't be at all surprised if earlier versions
didn't grok this.
--