template static data members

S

salem.ganzhorn

The following code compiles cleanly, but it looks like gcc 3.4.0 does
not emit the static data member into the object file (so I get a link
error):

#include <iostream>

template <class Type>
class foo {
public:
foo( Type i )
{
m_i = i;
}
void print() {
std::cout << m_i << std::endl;
}
protected:
static Type m_i;
};

template <> // should this be "template<int>"?
int foo<int>::m_i;

int main( int argc, const char** argv )
{
foo<int> f(3);
f.print();
}

Note that I do not understand the difference between prefixing the
instantiation of the "m_i" data member with "template <>" or "template
<int>". Both compile, neither one includes the storage for
foo<int>::m_i in the compilation unit.

Am I misusing the language? Is this a compiler bug?

TIA!

Salem
 
V

Victor Bazarov

The following code compiles cleanly, but it looks like gcc 3.4.0 does
not emit the static data member into the object file (so I get a link
error):

#include <iostream>

template <class Type>
class foo {
public:
foo( Type i )
{
m_i = i;
}
void print() {
std::cout << m_i << std::endl;
}
protected:
static Type m_i;
};

template <> // should this be "template<int>"?

No, it shouldn't
int foo<int>::m_i;

This is a specialisation of the member. Specialisation is not necessarily
a definition. In order for it to be a definition, you need to provide
an initialiser. I would say that you might want to specialise your static
template member if you are going to initialise it differently than the
default (implicitly instantiated) one. But the implicitly instantiated one
is not initialised in any way either (you didn't define it).

You might want to define the static member:

template said:
int main( int argc, const char** argv )
{
foo<int> f(3);
f.print();
}

Note that I do not understand the difference between prefixing the
instantiation of the "m_i" data member with "template <>" or "template
<int>". Both compile, neither one includes the storage for
foo<int>::m_i in the compilation unit.

Am I misusing the language? Is this a compiler bug?

Mmm.... I don't think it's a compiler bug. What were you trying to
accomplish with that declaration?

V
 
S

salem.ganzhorn

Victor Bazarov wrote:
Mmm.... I don't think it's a compiler bug. What were you trying to
accomplish with that declaration?

Victor,
I was trying to allocate storage for the static member. I guess the
answer is that I must have an initializer for the compiler to allocate
this storage.

Thanks!
Salem
 
V

Victor Bazarov

Victor Bazarov wrote:


Victor,
I was trying to allocate storage for the static member. I guess the
answer is that I must have an initializer for the compiler to allocate
this storage.

Beware, though, that for any other template argument (other than 'int')
you still don't have the definition for your static member, and the
linker should complain.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top