This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Initialization of a static member of a template class fails


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@adobe.com]
Envoyé : jeudi 26 juillet 2007 20:42
À : Valery Jean-Michel; gcc-help@gcc.gnu.org
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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]