[preprocessor] help generating create functions

Background: I am using a few functions which have the following form: void create ( ); template < typename P1 > void create ( P1 p1 ); template < typename P1, typename P2 > void create ( P1 p1, P2 p2 ); [...] I am trying to generate these using the boost preprocessor library, but I am getting an error. This is probably because I do not fully understand how the order of expansion of function-like macros works. The error is: warning C4002: too many actual parameters for macro 'BOOST_PP_IIF_1' My code is as follows: #define BOOST_PP_LOCAL_LIMITS (0, BOOST_SINGLETON_PTR_MAX_CONSTRUCTOR_PARAMS) #define BOOST_PP_LOCAL_MACRO(n) \ BOOST_PP_IF(n, template < BOOST_PP_ENUM_PARAMS(n, typename P) >, \ BOOST_PP_EMPTY()) void create ( BOOST_PP_ENUM_BINARY_PARAMS(n, \ typename ::boost::call_traits < P, >::param_type p ) ) \ { \ policy_ptr->get_creator ( )->create \ ( BOOST_PP_ENUM_PARAMS(n, p) ); \ } #include BOOST_PP_LOCAL_ITERATE() What am I doing wrong? -Jason

On 9/14/05, Jason Hise <chaos@ezequal.com> wrote:
Background: I am using a few functions which have the following form:
void create ( );
template < typename P1 > void create ( P1 p1 );
template < typename P1, typename P2 > void create ( P1 p1, P2 p2 ); [...]
I am trying to generate these using the boost preprocessor library, but I am getting an error. This is probably because I do not fully understand how the order of expansion of function-like macros works. The error is: warning C4002: too many actual parameters for macro 'BOOST_PP_IIF_1'
My code is as follows:
#define BOOST_PP_LOCAL_LIMITS (0, BOOST_SINGLETON_PTR_MAX_CONSTRUCTOR_PARAMS) #define BOOST_PP_LOCAL_MACRO(n) \ BOOST_PP_IF(n, template < BOOST_PP_ENUM_PARAMS(n, typename P) >, \ BOOST_PP_EMPTY()) void create ( BOOST_PP_ENUM_BINARY_PARAMS(n, \ typename ::boost::call_traits < P, >::param_type p ) ) \ { \ policy_ptr->get_creator ( )->create \ ( BOOST_PP_ENUM_PARAMS(n, p) ); \ } #include BOOST_PP_LOCAL_ITERATE()
What am I doing wrong?
Try replacing BOOST_PP_IF with BOOST_PP_EXPR_IF: BOOST_PP_EXPR_IF(n, template < BOOST_PP_ENUM_PARAMS(n, typename P) >)\ If I understand it correctly, BOOST_PP_EMPTY() expands to nothing before BOOST_PP_IF is called, causing BOOST_PP_IF to be called with: BOOST_PP_IF(n, template < BOOST_PP_ENUM_PARAMS(n, typename P) >,) Regards, Peder
-Jason
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Jason Hise
I am trying to generate these using the boost preprocessor library, but I am getting an error. This is probably because I do not fully understand how the order of expansion of function-like macros works. The error is: warning C4002: too many actual parameters for macro 'BOOST_PP_IIF_1'
My code is as follows:
#define BOOST_PP_LOCAL_LIMITS (0, BOOST_SINGLETON_PTR_MAX_CONSTRUCTOR_PARAMS) #define BOOST_PP_LOCAL_MACRO(n) \ BOOST_PP_IF(n, template < BOOST_PP_ENUM_PARAMS(n, typename P)
, \ BOOST_PP_EMPTY())
Two problems here... IF forwards its arguments to IIF, however, before it can do so, 1) ENUM_PARAMS is creating 'n' arguments and 2) EMPTY() is expanding to nothing. This definitely won't work. You need something like: IF(n, template < ENUM_PARAMS, TUPLE_EAT(2))(n, typename P) EXPR_IF(n, >)
void create ( BOOST_PP_ENUM_BINARY_PARAMS(n, \ typename ::boost::call_traits < P, >::param_type p ) ) \ { \ policy_ptr->get_creator ( )->create \ ( BOOST_PP_ENUM_PARAMS(n, p) ); \ } #include BOOST_PP_LOCAL_ITERATE()
What am I doing wrong?
The better way to go, IMO, is to take the non-template special case out of the generation algorithm--which simplies it. E.g. void create() { policy_ptr->get_creater()->create(); } #define LOCAL_LIMITS (1, MAX_PARAMS) #define LOCAL_MACRO(n) \ template< ENUM_PARAMS(n, typename P) > \ void create( \ ENUM_BINARY_PARAMS( \ n, \ typename ::boost::call_traits<P, >::param_type p \ ) \ ) \ { \ policy_ptr->get_creater()->create( \ ENUM_PARAMS(n, p) \ ); \ } \ /**/ #include LOCAL_ITERATE() Is there more to this code than what you actually have here? It seems like there should be. Regards, Paul Mensonides

Paul Mensonides wrote:
The better way to go, IMO, is to take the non-template special case out of the generation algorithm--which simplies it.
Yeah, that's probably the easiest option. I'll go with that.
Is there more to this code than what you actually have here? It seems like there should be.
There is indeed a bit more to it than I showed. I am using this technique in two separate places... the one I showed you was in the 'wrapping' layer, which defines the create functions as members of the outermost singleton smart pointer. These merely provide the interface and forward to the create functions generated inside the creator policy. A smart pointer to the creator policy instance is used for forwarding to take care of thread safety concerns. -Jason
participants (3)
-
Jason Hise
-
Paul Mensonides
-
Peder Holt