472,338 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,338 software developers and data experts.

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

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
Jul 22 '05 #1
8 1546
William Payne wrote:
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<typename T> T* foo<T>::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.


Of course.

Victor
Jul 22 '05 #2

"William Payne" <mi**************@student.liu.se> wrote in message
news:ck**********@news.island.liu.se...
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
Jul 22 '05 #3
* 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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4
John Harrison wrote:
"William Payne" <mi**************@student.liu.se> wrote in message
news:ck**********@news.island.liu.se...
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


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
Jul 22 '05 #5
William Payne wrote:
John Harrison wrote:
"William Payne" <mi**************@student.liu.se> wrote in message
news:ck**********@news.island.liu.se...
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


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
Jul 22 '05 #6

"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* 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 think

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

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

Jonathan
Jul 22 '05 #7
* Jonathan Turkanis:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:41*****************@news.individual.net...
* 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 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++.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #8
William Payne wrote:
William Payne wrote:
John Harrison wrote:
"William Payne" <mi**************@student.liu.se> wrote in message
news:ck**********@news.island.liu.se...

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


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/wg2...2003/n1426.pdf .

--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. ...
1
by: Vladimir Nesterovsky | last post by:
Hello, I have a code which behaves differently if template parameter has a specified member: #include <iostream> template<class T, bool...
3
by: salem.ganzhorn | last post by:
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):...
1
by: Frederiek | last post by:
Hi, When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an...
4
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) {...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better...
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ----...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.