By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,691 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,691 IT Pros & Developers. It's quick & easy.

template static data members

P: n/a
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

Jun 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sa************@gmail.com wrote:
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<class T> T foo<T>::m_i = 0;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 2 '06 #2

P: n/a

Victor Bazarov wrote:
<snip>

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?


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

Jun 2 '06 #3

P: n/a
sa************@gmail.com wrote:
Victor Bazarov wrote:
<snip>

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?


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.