template typename parameter cannot be "void"?
Hello,
I am wondering why "typename D" can be anything, a class or a
primitive type (int, float, etc.) but not void?
Now, in some situations, it would make sense to specialize a template
typename into "void".
I provide an example below. Actually, I am fetching an idiomatic way
to design a generic singleton factory that is able to create a single
instance of a class (C) either with or without initialization parameters
(D). My first idea fails (see line marked with comment "// KO").
Any suggestions?
Thanks, Sylvain GUILLEY.
template <class C, typename D> class singleton
{
static int nb_references;
static C* pinstance;
public:
static C* new_instance( D );
static void delete_instance();
};
// Initialize the reference count to zero
template <class C, typename D> int singleton<C,D>::nb_references = 0;
// Initialize pointer
template <class C, typename D> C* singleton<C,D>::pinstance = 0;
// Proxy to return the instance
template <class C, typename D> C* singleton<C,D>::new_instance( D init )
{
if( nb_references++ == 0 ) pinstance = new C( init );
return pinstance; // Address of sole instance
}
// Proxy to delete the instance
template <class C, typename D> void singleton<C,D>::delete_instance()
{
if( --nb_references == 0 ) delete pinstance;
}
// Class from which we would like to be able to call either ctor
struct a
{
a( int ) {}
a( void ) {}
};
int main()
{
a* Aint = singleton<a, int >::new_instance( 0 ); // OK
a* Avoid = singleton<a, void>::new_instance( void ); // KO
}