Issue with static members of templated classes

Eddie Parker eddie@replicon.com
Fri Dec 9 18:25:00 GMT 2005


Hello!

I'm encountering an issue with gcc 3.4.4, and I'm not sure how to proceed.

First off: I'm not assuming this is gcc's fault, as I'm not sure if what 
I'm trying to do is against what the spec for C++ says should be 
possible.  All I can say is that it worked on previous versions of gcc 
(which were less stringent on the spec, so that point is moot) and it 
works on MSVC (which also has varying levels of adherence to the spec, 
so that point is questionable at best as well).  I only mention this to 
let you know that I've attempted to see if this is workable on other 
systems, before submitting here.

Anyhow, this is a test case of my issue.  If you have three files, 
Foo.hpp, Foo.cpp, and main.cpp, with contents as such:


/**
   * Foo.hpp
   */
class Bar
{
};

template<class T>
class Foo
{
public:
    static T m_Member;
};

/**
   * Foo.cpp
   */
#include "Foo.hpp"

template<class T>
T Foo<T>::m_Member;

/**
   * main.cpp
   */

#include "Foo.hpp"

int main(void)
{
   Foo<Bar> foo;
   Bar &bar = foo.m_Member;
   return 0;
}

//---------------------- End.

When this is compiled and linked, I get:

main.cpp:10: undefined reference to 'Foo<Bar>::m_Member'

Now, the odd part is, is if I make the line:

   Bar &bar = foo.m_Member;

become:

   Bar bar = foo.m_Member;

Then everything compiles fine: no link error.

Why is the reference undefined one way, but not the other?

Any help would be appreciated! :)

-e-



More information about the Gcc-help mailing list