static member initialization in templates

G

gbs

Isn't this legal code to initialize a static member of a template
class?

template <class T>
class TemplatedClassT
{
static int staticMember_;
};


class A
{
//....
};

//...
//...initialize
//...
int TemplatedClassT<A>::staticMember_ = 0;

GCC 3.4.1 is rejecting it. But, how else could
TemplatedClassT<A>::staticMember_ be initialized?

Greg Silverman
Northrop Grumman
San Jose, CA
 
J

Jacek Dziedzic

gbs said:
Isn't this legal code to initialize a static member of a template
class?

template <class T>
class TemplatedClassT
{
static int staticMember_;
};


class A
{
//....
};

//...
//...initialize
//...
int TemplatedClassT<A>::staticMember_ = 0;

GCC 3.4.1 is rejecting it. But, how else could
TemplatedClassT<A>::staticMember_ be initialized?

Are you 100% sure that the initialization happens only once,
ie. that the file containing
int TemplatedClassT<A>::staticMember_ = 0;
is included only once?

HTH,
- J.
 
R

Rob Williscroft

Jacek Dziedzic wrote in in
comp.lang.c++:

The following is an explicit specialization (of a member),
It should be preceded with:

template said:
Are you 100% sure that the initialization happens only once,
ie. that the file containing
int TemplatedClassT<A>::staticMember_ = 0;
is included only once?

test.cpp:20: error: too few template-parameter-lists
test.cpp:20: error: expected `,' or `;' before '=' token

Rob.
 
?

=?ISO-8859-1?Q?Falk_Tannh=E4user?=

X-ReplaceAddress:yes
gbs said:
Isn't this legal code to initialize a static member of a template
class?

template <class T>
class TemplatedClassT
{
static int staticMember_;
};

template<typename T>
int TemplatedClassT<T>::staticMember_ = 0;

Falk Tannhäuser
 
G

Greg Comeau

Isn't this legal code to initialize a static member of a template
class?

template <class T>
class TemplatedClassT
{
static int staticMember_;
};


class A
{
//....
};

//...
//...initialize
//...
int TemplatedClassT<A>::staticMember_ = 0;

GCC 3.4.1 is rejecting it. But, how else could
TemplatedClassT<A>::staticMember_ be initialized?

By prefixing it with 'template <>'
 
B

Ben Hutchings

gbs said:
Isn't this legal code to initialize a static member of a template
class?
No.

template <class T>
class TemplatedClassT
{
static int staticMember_;
};


class A
{
//....
};

//...
//...initialize
//...
int TemplatedClassT<A>::staticMember_ = 0;

GCC 3.4.1 is rejecting it. But, how else could
TemplatedClassT<A>::staticMember_ be initialized?
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top