473,398 Members | 2,404 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,398 software developers and data experts.

static member initialization in templates

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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
5 3036
gbs wrote:
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.
Jul 22 '05 #2
Jacek Dziedzic wrote in news:ch**********@korweta.task.gda.pl in
comp.lang.c++:
gbs wrote:
Isn't this legal code to initialize a static member of a template
class?

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

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

template <>
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?


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

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
X-ReplaceAddress:yes
gbs wrote:
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #4
In article <69*************************@posting.google.com> ,
gbs <gb*@netbox.com> wrote:
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 <>'
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #5
gbs wrote:
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;
You must begin every full specialisation with "template<>". Just add
that at the beginning of this declaration.
GCC 3.4.1 is rejecting it. But, how else could
TemplatedClassT<A>::staticMember_ be initialized?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #6

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

Similar topics

3
by: Brian Ross | last post by:
Hi, I am having a problem writing a constructor/member initialization with VC.NET7.1. Here is the code: --- namespace Library
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
5
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
10
by: Jeffrey | last post by:
My understanding is that if you write class X { int y; static int z; }; then you've defined (and declared) X and y, but you have only declared (and not defined) z. If you'd like to...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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...

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.