Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-09-26 22:21:11

Simon Buchan wrote:

>Jason Hise wrote:
>>I am trying to provide a create member function of singleton_ptr which
>>forwards any parameters passed to it to the singleton's constructor.
>Hmm, that is tricky. Do you really think you could PP generate the
>overloads? (I haven't seen it in the Boost functional headers, but I
>didn't get too deep in them)
>The boost headers just use P1& (they call it A1) directly, but you could
>try something like:
>template <class P1>
>void create( P1 & p1 )
>{ do_create<call_traits<P1>::param_type>(p1);
>template <class P1>
>void create( P1 const & p1 ) // for rval arguments
>{ do_create<call_traits<P1>::param_type>(p1);
>template <class P1>
>void do_create( P1 p1 )
>(Disclaimer: not tested)
>Of course, this means for 9 arguments, you have 512 overloads to write,
>and that's not counting the extra do_create overloads! (Is this where PP
>magic could help?)
Creative approach, but unfortunately it defeats the purpose of the
call_traits optimization. I suppose I could just remove call traits
completely and always pass by const reference instead...

Btw, if you are interested this is what said code looks like in one part
of my singleton implementation:

// generate create functions taking any number of params that are
// forwarded to factory create function
template < BOOST_PP_ENUM_PARAMS(n, typename P) > \
void create ( BOOST_PP_ENUM_BINARY_PARAMS(n, const P, & p) ) \
{ \
    if ( ptr ) return; \
    ptr = factory.create ( BOOST_PP_ENUM_PARAMS(n, p) ); \


Boost list run by bdawes at, gregod at, cpdaniel at, john at