Article View: comp.std.c++
Article #2854Re: Initialization fo template static components?
From: Anil Vijendran
Date: Mon, 04 Dec 1995 16:30
Date: Mon, 04 Dec 1995 16:30
58 lines
2553 bytes
2553 bytes
>>>>> On 29 Nov 1995 18:11:36 GMT, solution@gate.net (Ken Walter)
said:
Ken> If I have a template class with static components, it would be nice if the
Ken> template instantiator also did likewise expanded static component
Ken> initializers. It is very awkward to require users of a template to initialize
Ken> private static components when I already know what they should be.
Ken> Example:
Ken> template< class T> class MYstuff
Ken> {
Ken> private:
Ken> static T* myPtr;
Ken> ..
Ken> }
Ken> I would like to say something like:
Ken> template <class T> T* MYstuff::myPtr = 0;
Ken> and have the compiler generate the initialization when it generates the
Ken> particular instance.
Ken> Anything like this possible or contemplated?
It is already there!
14.13 Static members and variables [temp.static]
2 Static class member templates are defined similarly to member function
templates. [Example:
template<class T> T X<T>::s = 0;
int X<int>::s = 3;
You could try this if your compiler doesn't support the above.
template <class T> class MyStuff
{
private:
static T* getMyPtr();
//...
};
template <class T> T* MyStuff<T>::getMyPtr() {
static T* myPtr;
// initialize myptr;
return myPtr;
}
--
Peace.... +<:-)
Anil
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<xg3pwe6fqhc.fsf@marsh.cacs.usl.edu>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageReferences:
<49i0v8$1q9m@news.gate.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: akv@cacs.usl.edu (Anil Vijendran) Newsgroups: comp.std.c++ Subject: Re: Initialization fo template static components? Date: 4 Dec 1995 16:30:28 GMT Organization: Center for Adv Computer Studies, Univ. of Southwestern Louisiana Sender: akv@marsh.cacs.usl.edu Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <xg3pwe6fqhc.fsf@marsh.cacs.usl.edu> References: <49i0v8$1q9m@news.gate.net> Reply-To: akv@cacs.usl.edu NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: marsh.cacs.usl.edu In-Reply-To: solution@gate.net's message of 29 Nov 1995 18:11:36 GMT Fcc: /home/swamp1/lakhotia/akv/.mail/.<cc-news> X-Newsreader: Gnus v5.0.12 Content-Length: 1487 X-Lines: 54 Originator: clamage@taumet