By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

Where does one initialize a static template member?

P: n/a
Consider the following class template, which has the static member
'seed_'. This template is intended to be inherited by several other
classes using the curiously recurring template pattern, so that each
class gets its own copy of 'seed_'. I am not sure where to initialize
the static member, though. Putting it in the header as indicated below
works in g++ 4.1, but I would like to know if this is just luck or if
the standard requires it to work (for example, some clause stating that
static template members can be multiply defined if every definition is
identical, or such).

The FAQ and Google haven't helped me too much, nor have I had any luck
finding the relevant clauses in the standard. Thanks for any help.
// auto_identifier.h
#include <ctime>

template <typename T>
class auto_identifier
{
public:
auto_identifier()
: counter_(++seed_), timestamp_(std::time(0))
{}

unsigned counter() const
{
return counter_ ;
}

std::time_t timestamp() const
{
return timestamp_ ;
}

static void seed(unsigned s)
{
seed_ = s ;
}
private:
unsigned counter_ ;
std::time_t timestamp_ ;

static unsigned seed_ ;
} ;

template <typename T>
unsigned auto_identifier<T>::seed_ = 0 ;

--
Alan Johnson
May 11 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
* Alan Johnson:
Consider the following class template, which has the static member
'seed_'. This template is intended to be inherited by several other
classes using the curiously recurring template pattern, so that each
class gets its own copy of 'seed_'. I am not sure where to initialize
the static member, though. Putting it in the header as indicated below
works in g++ 4.1, but I would like to know if this is just luck or if
the standard requires it to work (for example, some clause stating that
static template members can be multiply defined if every definition is
identical, or such).


Yep (it's required to work).

--
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?
May 11 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.