C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template Frage

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Micha Bieber
Guest





PostPosted: Tue May 04, 2004 3:47 pm    Post subject: Template Frage Reply with quote



Hallo group,

Ich habe hier ein kleines Reihenfolgeproblem:

--- foo.h ---
// includes

namespace N
{

template <class T>
class IO
{
public:
IO();
// ...

private:
struct Entry
{
// ...
};

typedef std::vector<Entry> Container;
static Container rlist_;
};

template <class T> N::IO<T>::Container N::IO<T>::rlist_ =
N::IO<T>::Container();

template <typename T>
IO<T>::IO()
{
if (notyethappened)
{
// setup the static rlist_
}
}

} // ns
--- foo.h ---


--- foo.cpp ---
#include "foo.h"

namespace{
static IO<N::SomeOtherClassInN> dummy;
}
--- foo.cpp ---

Was jetzt passiert ist, dass die Initialisierung des members rlist_
*nach* dem Anlegen von dummy erfolgt - und damit das setup in dessen
ctor hinfaellig macht. Ist das korrekt, oder mein Compiler (VC6) ?
Wie kann ich erzwingen, dass die member Initialisierung *vor* dem
Anlegen des statischen dummy Objekts erfolgt ?


Micha

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Stefan Reuther
Guest





PostPosted: Fri May 07, 2004 5:37 pm    Post subject: Re: Template Frage Reply with quote



Hallo,

Quote:
template <class T> N::IO<T>::Container N::IO<T>::rlist_ =
N::IO<T>::Container();
[...]
namespace{
static IO<N::SomeOtherClassInN> dummy;
}

Was jetzt passiert ist, dass die Initialisierung des members rlist_
*nach* dem Anlegen von dummy erfolgt - und damit das setup in dessen
ctor hinfaellig macht. Ist das korrekt, oder mein Compiler (VC6) ?

Die Template-Instanziierung landet in einer 'instantiation unit', und
die Reihenfolge, in der Initialisierungen aus verschiedenen 'translation
units' und 'instantiation units' ausgeführt werden, ist unspezifiziert.

Quote:
Wie kann ich erzwingen, dass die member Initialisierung *vor* dem
Anlegen des statischen dummy Objekts erfolgt ?

Du könntest Zugriffe auf 'dummy' über eine Accessor-Funktion regeln:
static IO<N::SomeOtherClassInN>&
getDummy()
{
static IO<N::SomeOtherClassInN> dummy;
return dummy;
}
Damit wird das Dummy erst erzeugt, wenn es benötigt wird. Aber das ist
nicht ganz das gleiche wie dein Original-Code. Eine andere Möglichkeit
kenne ich aber auch nicht.


Stefan

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Werner Salomon
Guest





PostPosted: Fri May 07, 2004 9:40 pm    Post subject: Re: Template Frage Reply with quote



Micha Bieber <mb_nospamplease_ (AT) traits (DOT) de> wrote

Quote:
Hallo group,

Ich habe hier ein kleines Reihenfolgeproblem:

--- foo.h ---
[...]
template <class T> N::IO<T>::Container N::IO<T>::rlist_ =
N::IO<T>::Container();
Hallo Micha,


dieser Ausdruck bewirkt rein gar nichts, da keine Instanz von T
angegeben ist.

Quote:
[...]
Was jetzt passiert ist, dass die Initialisierung des members rlist_
*nach* dem Anlegen von dummy erfolgt
nicht ganz - der "member" rlist_ wird nie (!) initialisiert. Zumindest

beim VC6-Compiler; ich meine aber auch, dass dieses Verhalten korrekt
ist.

Quote:
- und damit das setup in dessen
ctor hinfaellig macht. Ist das korrekt, oder mein Compiler (VC6) ?
Wie kann ich erzwingen, dass die member Initialisierung *vor* dem
Anlegen des statischen dummy Objekts erfolgt ?
Indem Du eine konkrete Klasse angibst, also in diesem Fall:

N::IO<SomeOtherClassInN>::Container N::IO<SomeOtherClassInN>::rlist_ =
N::IO<SomeOtherClassInN>::Container();

Gruß
Werner

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Olaf Krzikalla
Guest





PostPosted: Mon May 10, 2004 2:06 pm    Post subject: Re: Template Frage Reply with quote

Hi,

zuerstmal wäre zu klären, welche Aufgabe 'dummy' überhaupt hat. Soll an
der Stelle N::IO<N::SomeOtherClassInN> instantiiert werden, dann
empfehle ich explizite Instantiierung als Alternative:

template class N::IO<N::SomeOtherClassInN>;

Soll dummy jedoch eigentlich dazu dienen, ein setup über rlist_ zu
machen, so würde ich das in eine statische Funtion von IO packen und
diese statt des default-c'tors aufrufen.
Hat dummy noch irgendeine andere als eine der beiden vorstehenden
Aufgaben, so empfehle ich eine tiefergehende Beschreibung der Aufgabe
(nicht des Problems), denn vermutlich ist schon der dummy-Ansatz der
falsche Weg.


MfG
Olaf Krzikalla

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.