Can a template class have a static data member of type T?

W

William Payne

As the title says, is something like this legal:

template<typename T>
class foo
{
public:
static T* t;
};

T* foo::t;

?

I can't get it to compile, it doesn't like my definition of the static
member variable (I have it in the .cpp-file of the class). The compiler
simply says parse error before * token.

/ WP
 
V

Victor Bazarov

William said:
As the title says, is something like this legal:

template<typename T>
class foo
{
public:
static T* t;
};

T* foo::t;

No. 'T' is undefined. Change this to

template said:
?

I can't get it to compile, it doesn't like my definition of the static
member variable (I have it in the .cpp-file of the class). The compiler
simply says parse error before * token.

Of course.

Victor
 
J

John Harrison

William Payne said:
As the title says, is something like this legal:

template<typename T>
class foo
{
public:
static T* t;
};

T* foo::t;

?

Like this

template <typename T>
T* foo::t;

john
 
A

Alf P. Steinbach

* William Payne:
As the title says, is something like this legal:

template<typename T>
class foo
{
public:
static T* t;
};
OK.


T* foo::t;

Should be

template<typename T>
T* foo::t = something;

I can't get it to compile, it doesn't like my definition of the static
member variable (I have it in the .cpp-file of the class).

Unless T is restricted to one or a few types better place that definition
in the header file (and yes, that's supported by the standard).
 
W

William Payne

John said:
Like this

template <typename T>
T* foo::t;

john

Thanks John, Victor, and Alf for your quick reply. The solution makes
perfect sense to me because you have to do the same thing when defining
member function outside the (templated) class declaration).
I will soon be back, I think, with another question I see looming on the
horizon now that I can continue working on this program.

/ WP
 
W

William Payne

William said:
Thanks John, Victor, and Alf for your quick reply. The solution makes
perfect sense to me because you have to do the same thing when defining
member function outside the (templated) class declaration).
I will soon be back, I think, with another question I see looming on the
horizon now that I can continue working on this program.

/ WP

Hmm, I was a bit too quick to reply, it seems. I could only get Victor's
variant to compile:

template<typename T>
T* foo<T>::t;

Furthermore, I would get link errors if this definition wasn't in the
headers, but that applies to member functions as well (export, where are
you?).

/ WP
 
A

Alf P. Steinbach

* Jonathan Turkanis:
I think

template<typename T>
T* foo<T>::t;

is okay. Scalar types of static storage duration are zero-initialized.

Sorry for typo (forgot the "<T>" there).

Re initialization or not, initializing namespace level variables is IMO a Good
Habit.

I never remember the rules of declaration versus definition but an initialized
thing is definitely a definition, at least in C++.
 
J

Jonathan Turkanis

Alf P. Steinbach said:
* William Payne:

Should be

template<typename T>
T* foo::t = something;

I think

template<typename T>
T* foo<T>::t;

is okay. Scalar types of static storage duration are zero-initialized.

Jonathan
 
K

Karthik Kumar

William said:
Hmm, I was a bit too quick to reply, it seems. I could only get Victor's
variant to compile:

template<typename T>
T* foo<T>::t;

Furthermore, I would get link errors if this definition wasn't in the
headers,

Thatz how the current state-of-the-art is. You have to define
the templates in the header file.

but that applies to member functions as well (export, where are
you?).

/ WP

For that matter, export keyword is supported by *very few*
C++ compilers. Comeau compiler supports it.
This document provides interesting reading regarding admitting
'export' keyword in the standard.

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1426.pdf .
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top