I happened to come up with the following:
Replace the following lines in any_ptr.h in the existing article code:
93 static const int GenerateId() 94 { 95 static int id = 0; 96 return ++id; 97 } 98 99 template <class T>100 static const int TypeId()101 {102 static const int typeId = GenerateId();103 return typeId;104 }
with the following lines instead:
93 template <class T> 94 static const int TypeId() 95 { 96 static const int typeId( 0 ); 97 return reinterpret_cast<int>( &typeId ); 98 }
The idea behind the new code is that memory will be allocated at program start for each (static integral constant) typeId, belonging to each instantiation of the templated function TypeId. The amount of memory required to be allocated would be deducible at compile-time. Whether the initialization of typeId to zero is thread-safe or not doesn't matter because only its address is used, which serves as a unique identifier for a type.
Can anyone else clarify if this is a working solution?
Thanks in advance!
Francis Xavier