471,852 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,852 software developers and data experts.

Where does one initialize a static template member?

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
1 1821
* 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.

Similar topics

15 posts views Thread by cppaddict | last post: by
11 posts views Thread by Geoff Cox | last post: by
4 posts views Thread by Bram Kuijper | last post: by
5 posts views Thread by Bob Altman | last post: by
9 posts views Thread by Steven Woody | last post: by
5 posts views Thread by chgans | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

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.