473,543 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

As the title says, is something like this legal:

template<typena me 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 1587
William Payne wrote:
As the title says, is something like this legal:

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

T* foo::t;
No. 'T' is undefined. Change this to

template<typena me 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<typena me 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<typena me T>
class foo
{
public:
static T* t;
};
OK.

T* foo::t;
Should be

template<typena me 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<type name 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<typena me 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<typena me 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<typena me T>
class foo
{
public:
static T* t;
};


OK.

T* foo::t;


Should be

template<typena me T>
T* foo::t = something;


I think

template<typena me 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<typena me T>
class foo
{
public:
static T* t;
};


OK.

T* foo::t;


Should be

template<typena me T>
T* foo::t = something;


I think

template<typena me 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<typena me 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<typena me 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
2147
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 binary search tree. Note there is a requirement for this to be a Win32/MFC "friendly" class, thus the use of CObject and POSITION. There is also a...
7
2466
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
0
1626
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 internal interactive debugger that knows the classes within our system and allow us to walk around the objects that exist at the time of the...
1
1112
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
2106
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> class foo { public: foo( Type i )
1
2270
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 the data member is the same for every instance of that class? I would expect so, but I noticed something that makes me doubt a little.
5
3588
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 fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says ...
4
3345
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
2252
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 to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are...
5
3767
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 void UseMap (const std::string &key, int value)
0
7590
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7735
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7688
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5271
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4895
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
968
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.