Prev: C++ Redistributables
Next: Force loading of DLL
From: Ulrich Eckhardt on 1 Mar 2010 07:57 Nobody wrote: > int func(int seq, int para1,int para2) > { > // Some code > }; Just for the record, this isn't valid C or C++, as an empty statement (the semicolon after the function definition) is not valid at namespace scope. Ditto with the following code... > int func0(int para1,int para2) { return func(0,para1,para2);}; > int func1(int para1,int para2) { return func(1,para1,para2);}; > int func2(int para1,int para2) { return func(2,para1,para2);}; > int func3(int para1,int para2) { return func(3,para1,para2);}; > > > With Macros(func stays the same as above), no Boost library use: > > #define paster(n) int func##n(int para1,int para2) {return > func(n,para1,para2);} > > paster(0); > paster(1); > paster(2); > paster(3); This gets a price for ugliness and obfuscation. Generally accepted practice is to use ALL_UPPERCASE names for macros and only for those. > When setting up the call back, I use another macro to fill an array with > function pointers. Example: > > MYPROC ProcArray[APPMAX] = {0}; > > #define PasteProcArray(n) ProcArray[n] = (MYPROC) func##n; i++ Sheesh! Why? Why do you have to cast the function pointer? In any case, you can just replace this abomination with the not much more beautiful #define PasteProcArray(n) ProcArray[n] = (MYPROC) func<n>; i++ and save yourself the whole "paster(1)..." code above, as the template is automatically instantiated when used. > void InitProcArray(void) > { > int i=0; > > PasteInitProcArray(0); > PasteInitProcArray(1); > PasteInitProcArray(2); > PasteInitProcArray(3); > > if (i!=APPMAX) { > // Part of the array was not defined > MessageBox(NULL,_T("Size error in InitProcArray."), NULL, MB_OK); > } > } Imagine you have one PasteInitProcArray() line twice. Your check would say it's okay while one element is still missing. Actually, unless you need it somewhere else, you don't even have to dynamically initialize the array: MYPROC const ProcArray[] = { &func<0>, &func<1>, &func<2>, &func<3>, }; void InitProcArray() { assert((sizeof ProcArray)/(sizeof *ProcArray) == APPMAX); // nothing left to do } BTW: Your code looks more or less like C, are you using C or C++? I ask because without C++, any talk about templates is useless anyway. Cheers! Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Nobody on 1 Mar 2010 11:54 "Ulrich Eckhardt" <eckhardt(a)satorlaser.com> wrote in message news:e7qs57-ljd.ln1(a)satorlaser.homedns.org... > BTW: Your code looks more or less like C, are you using C or C++? I ask > because without C++, any talk about templates is useless anyway. I am using C++. I didn't know about template expression parameters. I assumed that even for templates that at least parameter types or their numbers
From: Nobody on 1 Mar 2010 11:59
"Nobody" <nobody(a)nobody.com> wrote in message news:uRICn$VuKHA.6124(a)TK2MSFTNGP04.phx.gbl... > "Ulrich Eckhardt" <eckhardt(a)satorlaser.com> wrote in message > news:e7qs57-ljd.ln1(a)satorlaser.homedns.org... >> BTW: Your code looks more or less like C, are you using C or C++? I ask >> because without C++, any talk about templates is useless anyway. > > I am using C++. I didn't know about template expression parameters. I > assumed that even for templates that at least parameter types or their > numbers Oops, clicked send too soon. What I meant to say is that I thought that templates must have at least a change in parameter types or their numbers just like overloaded functions. But apparently this is not the case with template functions. I will do more reading on that. Thank you all for your comments... |