Re: Am I or Alexandrescu wrong about singletons?
DeMarcus <use_my_alias_here@hotmail.com> writes:
<snip></snip>
I do the same as him. This is what he does.
<snip>Following code, at breaks in line numbering</snip>
Here's Singleton.h
00717 template
00718 <
00719 typename T,
00720 template <class> class CreationPolicy = CreateUsingNew,
00721 template <class> class LifetimePolicy = DefaultLifetime,
00722 template <class, class> class ThreadingModel =
00722b LOKI_DEFAULT_THREADING_NO_OBJ_LEVEL,
00723 class MutexPolicy = LOKI_DEFAULT_MUTEX
00724 >
00725 class SingletonHolder
00726 {
00727 public:
00733 static T& Instance();
00744 typedef typename
00744b ThreadingModel<T*,MutexPolicy>::VolatileType
00744c PtrInstanceType;
00745 static PtrInstanceType pInstance_;
00747 };
[...]
00775 // SingletonHolder::Instance
00777
00778 template
00779 <
00780 class T,
00781 template <class> class CreationPolicy,
00782 template <class> class LifetimePolicy,
00783 template <class, class> class ThreadingModel,
00784 class MutexPolicy
00785 >
00786 inline T& SingletonHolder<T, CreationPolicy,
00787 LifetimePolicy, ThreadingModel, MutexPolicy>::Instance()
00788 {
00789 if (!pInstance_)
00790 {
00791 MakeInstance();
00792 }
00793 return *pInstance_;
00794 }
Here's Threads.h containing ThreadingModel.
00252 template < class Host, class MutexPolicy =
00252b LOKI_DEFAULT_MUTEX >
00253 class ObjectLevelLockable
00254 {
00299 typedef volatile Host VolatileType;
00305 };
If you look at row 744b you see that he passes T* to ThreadingModel. If
you then look at row 299 you see that his VolatileType becomes a
volatile T*.
Where Host at line 299 is a T*, then VolatileType is not volatile T* but
rather T* volatile; that is, a volatile pointer to T.
He and me then do the exact same thing on row 793,
initializing a T& with a volatile T.
Rather, Alexandrescu is initializing a T& with an l-value of type T. It
is his T* that is volatile, not what it points to.
Regards
Paul Bibbings
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]