Re: Am I or Alexandrescu wrong about singletons?
Johannes Schaub (litb) wrote:
DeMarcus wrote:
Hi,
I try to implement a simplified version of Alexandrescu's
Loki::SingletonHolder. See
http://loki-lib.sourceforge.net/html/a00670.html
row 717.
My code looks like this.
template<typename T>
class Singleton
{
public:
static T& getInstance()
{
return *instance_;
}
private:
typedef volatile T* SPtr;
static SPtr instance_;
};
template<typename T>
typename Singleton<T>::SPtr Singleton<T>::instance_;
int main()
{
typedef Singleton<int> S;
S::getInstance() = 4711;
}
But when I compile it with gcc 4.4.1 I get the following error message
at 'return *instance_;'.
"error: invalid initialization of reference of type 'int&' from
expression of type 'volatile int' "
What am I doing wrong?
"T&" designates the type "int&" , but "*instance" is an expression of type
"volatile int". You cannot refer to a volatile object by a non-volatile
expression. If you do nontheless by casting away volatile, behavior is
undefined. The compiler guards you from that by not allowing the non-
volatile reference to bind to expressions of volatile type.
I dunno what Alexandrescu's code is doing, but surely there are more levels
of indirections in his code that care for health :)
That's the thing! I do the same as him. This is what he does.
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:
00728
00730 typedef T ObjectType;
00731
00733 static T& Instance();
00734
00735 private:
00736 // Helpers
00737 static void MakeInstance();
00738 static void LOKI_C_CALLING_CONVENTION_QUALIFIER
00738b DestroySingleton();
00739
00740 // Protection
00741 SingletonHolder();
00742
00743 // Data
00744 typedef typename
00744b ThreadingModel<T*,MutexPolicy>::VolatileType
00744c PtrInstanceType;
00745 static PtrInstanceType pInstance_;
00746 static bool destroyed_;
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 {
00255 mutable MutexPolicy mtx_;
00256
00257 public:
00258 ObjectLevelLockable() : mtx_() {}
00259
00260 ObjectLevelLockable(const ObjectLevelLockable&) :
00260b mtx_() {}
00261
00262 ~ObjectLevelLockable() {}
00263
00264 class Lock;
00265 friend class Lock;
00266
00269 class Lock
00270 {
00271 public:
00272
00274 explicit Lock(const ObjectLevelLockable& host) :
00274b host_(host)
00275 {
00276 host_.mtx_.Lock();
00277 }
00278
00280 explicit Lock(const ObjectLevelLockable* host) :
00280b host_(*host)
00281 {
00282 host_.mtx_.Lock();
00283 }
00284
00286 ~Lock()
00287 {
00288 host_.mtx_.Unlock();
00289 }
00290
00291 private:
00293 Lock();
00294 Lock(const Lock&);
00295 Lock& operator=(const Lock&);
00296 const ObjectLevelLockable& host_;
00297 };
00298
00299 typedef volatile Host VolatileType;
00300
00301 typedef LOKI_THREADS_LONG IntType;
00302
00303 LOKI_THREADS_ATOMIC_FUNCTIONS
00304
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*. He and me then do the exact same thing on row 793,
initializing a T& with a volatile T.
I tried to implement my own singleton by means of Modern C++ Design by
Alexandrescu (an excellent book by the way), but I got stuck when my
compiler started to complain. Actually, in the book in Section 6.10.3
Assembling SingletonHolder, p.151 he writes "The
ThreadingModel<T>::VolatileType type definition expands either to T or
volatile T, depending on the actual threading model."
Could it be that the compilers at the time he wrote the SingletonHolder
let through that volatile conversion, but now they don't?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]