Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

initialization/lifetimes of static class members...

0 views
Skip to first unread message

werasm

unread,
Jul 17, 2006, 4:11:39 PM7/17/06
to
Hi all,

I could not get clarity wrt. from std 98 and wondered whether you could
point me to where I could find the following (see example):

//x_c.h
class x_c
{
private:
static x x1;
static x x2;
};

//x_c.cpp
// Note order of definition swapped around as compared to order of
//declaration in x_c...
x x_c::x2;
x x_c::x1;

Now, given the above scenario, par 3.6.2/1 states:

Objects with static storage duration defined in the namespace scope in
the same translation unit and dynamically initialized shall be
initialized in the order in which their definition appears in the
translation unit.

I consider the cpp file to hold the definition of the static members
(9.4.2/2). Therefore x2 would have a longer lifetime than x1, right -
as it is defined before x1 in the translation unit?

This may seem to be contradictory to normal class members, where their
lifetime is determined by their order of declaration in the class. My
guess wrt. the contradiction (if I'm right wrt. static member
initialization order) is that the declaration of non-static data
members are realized (become definitions) when the class is defined (at
its closing brace of its class specifier - 9.2). This is not true for
static data members, hence the contradiction :-)

Is my thinking correct wrt. the above?

Kind regards,

Werner


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

Victor Bazarov

unread,
Jul 17, 2006, 10:38:14 PM7/17/06
to

Not from where I'm standing. There is no *contradiction*. Orders
of initialisation are different for static and non-static members.
Nothing else. Where's contradiction in that? Member functions can
be overloaded and member variables cannot. Is there a contradiction
in that? I think you misunderstand what "contradiction" means.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

werasm

unread,
Jul 18, 2006, 9:46:05 AM7/18/06
to

Victor Bazarov wrote:

> Not from where I'm standing. There is no *contradiction*. Orders
> of initialisation are different for static and non-static members.
> Nothing else. Where's contradiction in that? Member functions can
> be overloaded and member variables cannot. Is there a contradiction
> in that? I think you misunderstand what "contradiction" means.

I said it seemed to be contradictory. I did not say pertinently that it
was. Look at it from this perpective.

struct x_c
{
x_c();
private:
static x x1_;
static x x2_;
m m1_;
m m2_;
};

//cpp
// Initialisation not iaw. declaration, but iaw. definition in
// compilation unit, therefore x2_ really initialized before x1_.
x x_c::x2_;
x x_c::x1_;

x_c::x_c()
: m2_(), m1_() //initialisation iaw. order of declaration,
// which is why m2_ is initialised after m1_ - carefull :-)
{

}

Not contradictory, but certainly seems that way.

As I said, I can see why it could be anticipated as contradictory. OTOH
I can also see why it is not (If my understanding is correct).

Now, again my question which you did not answer (which I BTW notice you
often do for many OPs) - is my understanding of this correct?

Thank you

W

kanze

unread,
Jul 18, 2006, 7:11:13 PM7/18/06
to
werasm wrote:

> I could not get clarity wrt. from std 98 and wondered whether
> you could point me to where I could find the following (see
> example):

> //x_c.h
> class x_c
> {
> private:
> static x x1;
> static x x2;
> };

> //x_c.cpp
> // Note order of definition swapped around as compared to order of
> //declaration in x_c...
> x x_c::x2;
> x x_c::x1;

> Now, given the above scenario, par 3.6.2/1 states:

> Objects with static storage duration defined in the namespace
> scope in the same translation unit and dynamically initialized
> shall be initialized in the order in which their definition
> appears in the translation unit.

> I consider the cpp file to hold the definition of the static
> members (9.4.2/2). Therefore x2 would have a longer lifetime
> than x1, right - as it is defined before x1 in the translation
> unit?

Exactly.

> This may seem to be contradictory to normal class members,
> where their lifetime is determined by their order of
> declaration in the class.

In both cases, order is deterimined by the order of definition.
Non static members are defined in the class definition; static
members no.

> My guess wrt. the contradiction (if I'm right wrt. static
> member initialization order) is that the declaration of
> non-static data members are realized (become definitions) when
> the class is defined (at its closing brace of its class
> specifier - 9.2). This is not true for static data members,
> hence the contradiction :-)

> Is my thinking correct wrt. the above?

I think so.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Victor Bazarov

unread,
Jul 18, 2006, 7:23:45 PM7/18/06
to
werasm wrote:
> [..]

> Now, again my question which you did not answer

I did. You must have missed it. The answer is implied in the
"there is NO contradition" (change of emphasis). So, the answer
was "NO, your thinking is incorrect".

> (which I BTW notice
> you often do for many OPs)

Huh?

> - is my understanding of this correct?

WHAT is correct? That it seems contradictory to you? That it
could seem contradictory (to somebody else reading the Standard)?
Or that initialisation order is in fact different for static
objects and non-static members?

I don't see any room for interpretation in the way the Standard
defines member initialisation. Do you? I don't therefore see
any way to misunderstand. Statics are initialised in the order
they appear *defined* in the TU. Non-static data members are
initialised in the order they are declared in the class
definition (you could call that "defined"). What *understanding*
can be incorrect here?

Your question (I *allegedly* didn't answer) was: "Is my thinking
correct wrt. the above?"

How in all fairness can I answer that? Which of "the above" is
your thinking correct "wrt."? I answered the statement "hence
the contradition". Everything else (whether your thinking is
correct, whatever) please deduce from my reply.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

[ See http://www.gotw.ca/resources/clcm.htm for info about ]

john...@yahoo.com

unread,
Jul 19, 2006, 3:28:11 PM7/19/06
to
werasm wrote:

> This may seem to be contradictory to normal class members, where their
> lifetime is determined by their order of declaration in the class. My
> guess wrt. the contradiction (if I'm right wrt. static member
> initialization order) is that the declaration of non-static data
> members are realized (become definitions) when the class is defined
> (at
> its closing brace of its class specifier - 9.2). This is not true for
> static data members, hence the contradiction :-)
>
> Is my thinking correct wrt. the above?
>

Pretty close, but I wouldn't put it quite that way.

All declarations are definitions, unless they fit within the specific
exceptions enumerated in 3.1/2. Static data member declarations are
among the specific exceptions listed. I don't think the closing brace
of the class definition plays a role here.

And the general rule, as you've surmised, is that objects are
initialized in the order in which they are defined. The presence and
ordering of any non-defining declarations does not affect this.

0 new messages