473,409 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 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 1582
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 improvements. This is a template class for a...
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 <typename T> class M { static Registrar<M>...
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. Essentially at the point of the error, we invoke an...
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 (T::*)()> class member_bind {
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): #include <iostream> template <class Type>...
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 the class. Does that mean that the address 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 instance of the class SpectralProfile, it compiles...
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 ) { //default ToString handles integers/doubles
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 luck :) The standard explanation is that pointer...
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: ---- template<typename T> class BaseT { public: static...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.