Boost logo

Boost :

From: Simon Buchan (simon_at_[hidden])
Date: 2005-09-27 00:01:01


Jason Hise wrote:
> 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...
>
Actually, the compiler will inline out 'create', and do_create will get
the right parameter types. (byval if not big, etc...)
if do_create just took [const] reference, and the compiler didn't know
anything about the c'tor (quite possible), it would be forced to
implement every parameter by reference.
You have to ask yourself if this optimisation is really worth it,
though, at most you get a few more pointer dereferrencing levels than
you wanted.
> 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
> #define BOOST_PP_LOCAL_LIMITS (1, \
> BOOST_SINGLETON_PTR_MAX_CONSTRUCTOR_PARAMS)
> #define BOOST_PP_LOCAL_MACRO(n) \
> 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) ); \
> }
> #include BOOST_PP_LOCAL_ITERATE()
>
Wow... Even after reading the docs I can't understand that :D
I assume it expands to:
template < P1, P2, .. Pn >
void create ( const P1& p1, const P2& p2, ... const Pn& pn )
{
     if ( ptr ) return;
     ptr = factory.create ( p1, p2, ... pn );
}
right? What about nullary c'tors? Is there any way to do O(n^2)
declarations like T const& and T& overloads for all parameters?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk