6 msgtop-level configure options, --help
2 msgAnother Gcc 4.2.2 Bug
2 msgGcc Patch Archive?
10 msgCompile gcc from source on a 64 bit OS ?
4 msgRe: help compiling GCC in Fedora 8
10 msgconfigure: error: No support for this target/ho...
5 msgCan `cpp' strip off comments in C source code?
2 msgQuestion On Testsuite 12/22/07
2 msgQuestion: How dose Gcc choose which Ld and As t...
2 msgneed an explanation on assembly generated by va...
3 msgI can not resolve 'error: conflicting types for...
8 msga question about code optimization
7 msgAn error when trying to gmake install GCC 3.4.6...

RE: Initialization of a static member of a temp...
\ Valery Jean-Michel (21 Dec 2007)
. \ John Love-Jensen (21 Dec 2007)

9 msgvoid pointer and map
3 msgAnother Gcc-4.1.2 Bug On Solaris 9 12/20/07
2 msgHelp with building/using GCC cross compiler to ...
1 msgBootstrapping gcc-4.2.2 with binutils-2.18
2 msginline assembly and segment registers
1 msgMultiple names for gcc inline assembly constrai...
Subject:RE: Initialization of a static member of a template class fails
Group:Gcc-help
From:Valery Jean-Michel
Date:21 Dec 2007


Dear John,

thank you for your help. I'm terribly soory to reply so late but I have to
switch urgently to another project, just after posting my problem, leaving
it as is.
Hopefully, I'm now back on it. I have tried your solution: the compilation
was successfull but a crash occured at run time.
So, I have applied the singleton design pattern to my class with:
the following attribute:
static instances_list *s_instances;
the constructor:
Enum(int Value): m_value(Value) {
if (!s_instances) s_instances = new instances_list;
s_instances->insert(this);
};
and the initialization of static members
template <class T> typename Enum<T>::instances_list * Enum<T>::s_instances =
NULL;

It works fine now.
Thanks again or your help.

I have another question because I don't really understand what happens.
Please consider the following files:

CTemp1.h containing:
#ifndef __CTEMP1
#define __CTEMP1

template <typename T> class CTemp1
{
public:

int m_CTemp1;
int CTemp1_affecter() {
m_CTemp1 = 2;
return 2;
}
};
#endif

CTemp2.h containing:
#ifndef __CTEMP2
#define __CTEMP2

#include "CTemp1.h"

template <typename T> class CTemp2: public CTemp1<T>
{
public:
T m_CTemp2;
inline int CTemp2_affecter() {
#ifdef USE_THIS
this->m_CTemp1 = 3;
#else
m_CTemp1 = 3;
#endif
m_CTemp2 = 3;
};
};

#endif


Main.cpp containing:
#include "CTemp2.h"

int main(int argc , char ** argv)
{

CTemp2<int> t2;

t2.CTemp2_affecter();
t2.m_CTemp1 = 3;
return 0;
};

I don't undertand why with the latest release (3.4.6) of gcc we use (it was
OK with 3.2.3 release), Main.cpp doesn't compile if I don't define USE_THIS:
$ g++ -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-java-awt=gtk
--host=i386-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)
$ g++ Main.cpp
In file included from Main.cpp:1:
CTemp2.h: In member function `int CTemp2<T>::CTemp2_affecter()':
CTemp2.h:14: error: `m_CTemp1' was not declared in this scope
$ g++ -DUSE_THIS Main.cpp
$

In fact, I don't undertand why the line t2.m_CTemp1 = 3; is correct in
Main.cpp and not m_CTemp1 = 3; in CTemp2_affecter().
Thanks by advance if you have any idea.
I wish you all a Merry Christmas and an Happy New Year.
Best regards.

Jean-Michel

-----Message d'origine-----
De : John (Eljay) Love-Jensen [mailto:eljay]
Envoyé : jeudi 26 juillet 2007 20:42
À : Valery Jean-Michel; gcc-help
Objet : RE: Initialization of a static member of a template class fails


Hi Valery,

I suspect your code is running into the dreaded Order Of Construction
problem. Which is only turn-of-the-screw less painful than the even more
dreaded Order Of Destruction problem.

I replaced this part of your code...

static instance_list s_instances;

...with a class function to access a function-wrapped static instance_list
variable...

static instance_list& GetInstanceList()
{
static instance_list s_instances;
return s_instances;
}

...and the crasher (which I also saw) went away.

Of course, you won't want a class static accessor function wrapping an
instance variable in a header file, since that may cause even harder to
diagnose problems. Such as: every translation unit* getting one-or-more
copies of an inlined GetInstanceList. Doh!

The solution to that latter problem is rather heinous (in my opinion), which
involves explicit instantiation for all known instances of Enum<T> in a
designated translation unit. Ugh. Maybe there is a more elegant solution.

HTH,
--Eljay

* Or compilation unit - whatever your terminological preference.


© 2004-2008 readlist.com