Static members in Templates 
Author Message
 Static members in Templates

Hi!

I got the following:

-----8<-----8<-----8<-----8<-----8<-----
template <class T>
class Test
{
public:
 static T Value;

Quote:
};

int main()
{
 Test<int>::Value=2;
 Test<int> Blah;
 Blah.Value=1;
 return 0;
Quote:
}

-----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?

Tanks in advance!

In this sense - Richie



Sat, 21 Dec 2002 03:00:00 GMT  
 Static members in Templates
Greets,

    One will want to move the static initialization outside of the main and
be sure to specify the type for the static variable as well.  The following
should yield the desired result:


Quote:
> Hi!

> I got the following:

> -----8<-----8<-----8<-----8<-----8<-----
> template <class T>
> class Test
> {
> public:
>  static T Value;
> };

> 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?

> Tanks in advance!

> In this sense - Richie



Sat, 21 Dec 2002 03:00:00 GMT  
 Static members in Templates

Greets,

    One will want to initialize the static member outside of the template
and also use the appropriate type when doing such.  The following should
yield the desired result:

-----------------------

#include <iostream>

template <class T>
class Test
{
public:
    Test() {}   /* do away with unreferenced local variable warning */
public:
    static T Value;

Quote:
};

int Test<int>::Value=1;
char Test<char>::Value='a';

int main()
{
    using std::cout;
    using std::endl;

    cout << Test<int>::Value << endl;
    cout << Test<char>::Value << endl;

    Test<int> oIntObject;
    Test<char> oCharObject;

    cout << oIntObject.Value << endl;
    cout << oCharObject.Value << endl;

    oIntObject.Value=2;
    oCharObject.Value='b';

    cout << oIntObject.Value << endl;
    cout << oCharObject.Value << endl;

    return(0);

Quote:
}

---------------------------------

Regards,

Joe


Quote:
> Hi!

> I got the following:

> -----8<-----8<-----8<-----8<-----8<-----
> template <class T>
> class Test
> {
> public:
>  static T Value;
> };

> 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?

> Tanks in advance!

> In this sense - Richie



Sat, 21 Dec 2002 03:00:00 GMT  
 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.

--



Sun, 19 Jan 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. static members in template class across DLL's

2. VC7 bug: static member templates

3. template classes with static members

4. static data member of template class

5. Initialization of static data member of template class

6. template classes with static members

7. Static member definition of template class fails

8. Static member definition of template class fails

9. Static member variables of template class

10. static member objects of templates - bug?

11. Unresolved static template data member exported from DLL

12. dllexport instantiated static template member

 

 
Powered by phpBB® Forum Software