Re: Problem with Singleton template (2)
On 22 Aug, 07:48, ivnic...@yahoo.com wrote:
so I put together some code for you below. the trivial singleton
example is not thread safe and also doesn't come with any lifecycle or
creation/destruction policy. I just wanted to show you how a class
hierarchy can be used together with a singleton, that's it.
Thanks for your reply and code - they were really helpful.
Unfortunately, our TI DSP compiler is not fully ANSI C++ Compliant.
The
singleton template generates a compiler warning:
static T * instance( )
{
static T t; <<======== Line 52
return &t;
}
"../Source/Singleton.h", line 52: warning: static local variables of
extern
inline function are not resolved to single copy. Not ANSI C++
Compliant
TI's explanation is:
"Static Variables in Inline Functions: Things are even more difficult
when the
inline function contains a static variable. The presence of a static
variable
does not inhibit inlining. The ANSI standard for C++ requires the
compiler to
create a single instance of the static local in such a case. The TI
compiler
does not adhere to this part of the standard. It creates a static copy
of the
local variable similar to how the function itself is made static.
While it
rarely occurs in practice, this can cause code to execute incorrectly.
If the inline function is a class member function, then the best
workaround is
to make the local static variable a static member variable of the
class."
However, it seems to me that the workaround will break the Meyers
Singleton
pattern. To resolve this I changed back to dynamic instantiation of
the
instance variable:
static T * instance( ) {
if (mp_instance == 0) mp_instance = new T();
return mp_instance;
}
private:
static T* mp_instance;
However, I then get a linker error:
<Linking>
undefined symbol first
referenced in
----------------
-------------------
Singleton<T1>::mp_instance [with T1=SchedulerOpList<int>] C:\
\...
error: unresolved symbols remain
Please can you suggest how I can resolve this linker error?
Best regards
David
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]