Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > template typena...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 43070 of 47561
Post > Topic >>

template typename parameter cannot be "void"?

by Sylvain Guilley <sylvain.guilley@[EMAIL PROTECTED] > Feb 3, 2008 at 02:35 PM

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
}
 




 4 Posts in Topic:
template typename parameter cannot be "void"?
Sylvain Guilley <sylva  2008-02-03 14:35:02 
Re: template typename parameter cannot be "void"?
peter koch <peter.koch  2008-02-03 05:50:07 
Re: template typename parameter cannot be "void"?
Sylvain Guilley <sylva  2008-02-03 19:08:34 
Re: template typename parameter cannot be "void"?
tragomaskhalos <dave.d  2008-02-03 15:27:26 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Sep 7 4:00:58 CDT 2008.