|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I was asked whether it is possible to prepend a name (using macro
substitution) to all function names in a file/program. For example, the functions f1(), f2(), f3() should get aa_ prepended to their names? I.e., f1(), f2(), f3() should be changed to aa_f1(), aa_f2(), aa_f3() using some macro substitution. I came up with the following: ************************************************** ** #include <stdio.h> #define PREPEND aa_ #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name void MAKE_FUNC_NAME(PREPEND, f1)(void) { printf("here..\n"); } ************************************************** ** However, this does not define the function aa_f1, instead the function PREPENDf1 is defined. Why? Is there some way to get around this to achieve what is set out to be done? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
dspfun <dspfun@hotmail.com> writes:
> #define PREPEND aa_ > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name > > void MAKE_FUNC_NAME(PREPEND, f1)(void) > { > printf("here..\n"); > } > ************************************************** ** > > However, this does not define the function aa_f1, instead the function > PREPENDf1 is defined. Why? Is there some way to get around this to > achieve what is set out to be done? This is in the C FAQ: 11.17: I'm trying to use the ANSI "stringizing" preprocessing operator `#' to insert the value of a symbolic constant into a message, but it keeps stringizing the macro's name rather than its value. A: You can use something like the following two-step procedure to force a macro to be expanded as well as stringized: #define Str(x) #x #define Xstr(x) Str(x) #define OP plus char *opname = Xstr(OP); This code sets opname to "plus" rather than "OP". An equivalent circumlocution is necessary with the token-pasting operator ## when the values (rather than the names) of two macros are to be concatenated. References: ISO Sec. 6.8.3.2, Sec. 6.8.3.5. -- char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[] ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p =b,i=24;for( +=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}} |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote:
> dspfun <dsp...@hotmail.com> writes: > > #define PREPEND aa_ > > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name > > > void MAKE_FUNC_NAME(PREPEND, f1)(void) > > { > > printf("here..\n"); > > } > > ************************************************** ** > > > However, this does not define the function aa_f1, instead the function > > PREPENDf1 is defined. Why? Is there some way to get around this to > > achieve what is set out to be done? > > This is in the C FAQ: > > 11.17: I'm trying to use the ANSI "stringizing" preprocessing operator > `#' to insert the value of a symbolic constant into a message, > but it keeps stringizing the macro's name rather than its value. > It's almost the same, but not quite. What I am trying to do is to create the function name using macro substitution, however, the problem is that the PREPEND is never substituted for its corresponding #define inside the MAKE_FUNC_NAME macro. I.e. MAKE_FUNC_NAME(PREPEND, f1)(void); is by the preprocessor substituted to: PREPENDf1(void); What I am trying to achieve is to get MAKE_FUNC_NAME(PREPEND, f1) (void) substituted to: aa_f1(void); Then if for example one have 100 functions, f1()...f100, it is easy to just change the #define to get all functions named with aa_ , bb_ etc. prepended to them. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Tue, 29 Jan 2008 12:44:29 -0800, dspfun wrote:
> On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote: >> dspfun <dsp...@hotmail.com> writes: >> > #define PREPEND aa_ >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name >> >> > void MAKE_FUNC_NAME(PREPEND, f1)(void) { >> > Âprintf("here..\n"); >> > } >> > ************************************************** ** >> >> > However, this does not define the function aa_f1, instead the >> > function PREPENDf1 is defined. Why? Is there some way to get around >> > this to achieve what is set out to be done? >> >> This is in the C FAQ: >> >> 11.17: ÂI'm trying to use the ANSI "stringizing" preprocessing operator >> Â Â Â Â `#' to insert the value of a symbolic constant into a >> Â Â Â Â message, but it keeps stringizing the macro's name rather >> Â Â Â Â than its value. >> >> > It's almost the same, but not quite. [...] Correct. However, the fix for the ## operator is exactly the same as the fix for the # operator: add an extra macro. PREPEND gets expanded as long as you don't perform the concatenation in MAKE_FUNC_NAME itself, like so: #define PREPEND aa_ #define MAKE_FUNC_NAME_ER(x, y) x ## y #define MAKE_FUNC_NAME(prepend, func_name) MAKE_FUNC_NAME_ER(prepend, func_name) void MAKE_FUNC_NAME(PREPEND, f1)(void) { printf("here..\n"); } |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 29 Jan, 21:55, Harald van Dijk <true...@gmail.com> wrote:
> On Tue, 29 Jan 2008 12:44:29 -0800, dspfun wrote: > > On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote: > >> dspfun <dsp...@hotmail.com> writes: > >> > #define PREPEND aa_ > >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name > > >> > void MAKE_FUNC_NAME(PREPEND, f1)(void) { > >> > Âprintf("here..\n"); > >> > } > >> > ************************************************** ** > > >> > However, this does not define the function aa_f1, instead the > >> > function PREPENDf1 is defined. Why? Is there some way to get around > >> > this to achieve what is set out to be done? > > >> This is in the C FAQ: > > >> 11.17: ÂI'm trying to use the ANSI "stringizing" preprocessing operator > >>     `#' to insert the value of a symbolic constant into a > >>     message, but it keeps stringizing the macro's name rather > >>     than its value. > > > It's almost the same, but not quite. [...] > > Correct. However, the fix for the ## operator is exactly the same as the > fix for the # operator: add an extra macro. PREPEND gets expanded as long > as you don't perform the concatenation in MAKE_FUNC_NAME itself, like so: > > #define PREPEND aa_ > #define MAKE_FUNC_NAME_ER(x, y) x ## y > #define MAKE_FUNC_NAME(prepend, func_name) MAKE_FUNC_NAME_ER(prepend, > func_name) > > void MAKE_FUNC_NAME(PREPEND, f1)(void) { >  printf("here..\n"); > > > > }- Dölj citerad text - > > - Visa citerad text -- Dölj citerad text - > > - Visa citerad text - Ok, thanks! What is the reason it's not possible to perform the conatenation in MAKE_FUNC_NAME itself? Which sentence(s) in the standard (n1256.pdf) specifies this? |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Tue, 29 Jan 2008 13:12:49 -0800, dspfun wrote:
> On 29 Jan, 21:55, Harald van Dijk <true...@gmail.com> wrote: >> >> dspfun <dsp...@hotmail.com> writes: >> >> > #define PREPEND aa_ >> >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name [quoted text below edited for readability] >> #define PREPEND aa_ >> #define MAKE_FUNC_NAME_ER(x, y) x ## y >> #define MAKE_FUNC_NAME(prepend, func_name) \ >> MAKE_FUNC_NAME_ER(prepend, func_name) > > Ok, thanks! What is the reason it's not possible to perform the > conatenation in MAKE_FUNC_NAME itself? The rationale states: "The specification of this pasting operator is based on these principles: [...] * A formal parameter as an operand for ## is not expanded before pasting. The actual parameter is substituted for the formal parameter; but the actual parameter is not expanded. Given, for example #define a(n) aaa ## n #define b 2 the expansion of a(b) is aaab, not aaa2 or aaan. [...] These principles codify the essential features of prior art and are consistent with the specification of the stringizing operator." In other words, there was no specific reason for or against expansion of macro arguments, and existing implementations didn't expand macro arguments, so that's what got into the standard. > Which sentence(s) in the standard > (n1256.pdf) I expect you'll get comments focusing solely on this. > specifies this? The standard specifies this in 6.10.3.1p1: "After the arguments for the invocation of a function-like macro have been identified, argument substitution takes place. A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded." |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On 29 Jan, 22:39, Harald van D©¦k <true...@gmail.com> wrote:
> On Tue, 29 Jan 2008 13:12:49 -0800, dspfun wrote: > > On 29 Jan, 21:55, Harald van D©¦k <true...@gmail.com> wrote: > >> >> dspfun <dsp...@hotmail.com> writes: > >> >> > #define PREPEND aa_ > >> >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name > > [quoted text below edited for readability] > > >> #define PREPEND aa_ > >> #define MAKE_FUNC_NAME_ER(x, y) x ## y > >> #define MAKE_FUNC_NAME(prepend, func_name) \ > >> MAKE_FUNC_NAME_ER(prepend, func_name) > > > Ok, thanks! What is the reason it's not possible to perform the > > conatenation in MAKE_FUNC_NAME itself? > > The rationale states: > > "The specification of this pasting operator is based on these principles: > [...] > * A formal parameter as an operand for ## is not expanded before pasting. > The actual parameter is substituted for the formal parameter; but the > actual parameter is not expanded. Given, for example > #define a(n) aaa ## n > #define b 2 > the expansion of a(b) is aaab, not aaa2 or aaan. > [...] > These principles codify the essential features of prior art and are > consistent with the specification of the stringizing operator." > > In other words, there was no specific reason for or against expansion of > macro arguments, and existing implementations didn't expand macro > arguments, so that's what got into the standard. > > > Which sentence(s) in the standard > > (n1256.pdf) > > I expect you'll get comments focusing solely on this. > > > specifies this? > > The standard specifies this in 6.10.3.1p1: > "After the arguments for the invocation of a function-like macro have > been identified, argument substitution takes place. A parameter in the > replacement list, unless preceded by a # or ## preprocessing token or > followed by a ## preprocessing token (see below), is replaced by the > corresponding argument after all macros contained therein have been > expanded." Thank you very much! |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
dspfun <dspfun@hotmail.com> writes:
> On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote: >> dspfun <dsp...@hotmail.com> writes: >> > #define PREPEND aa_ >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name >> >> > void MAKE_FUNC_NAME(PREPEND, f1)(void) >> > { >> > printf("here..\n"); >> > } >> > ************************************************** ** >> >> > However, this does not define the function aa_f1, instead the function >> > PREPENDf1 is defined. Why? Is there some way to get around this to >> > achieve what is set out to be done? >> >> This is in the C FAQ: >> >> 11.17: I'm trying to use the ANSI "stringizing" preprocessing operator >> `#' to insert the value of a symbolic constant into a message, >> but it keeps stringizing the macro's name rather than its value. >> > > It's almost the same, but not quite. What I am trying to do is to > create the function name using macro substitution, however, the > problem is that the PREPEND is never substituted for its corresponding > #define inside the MAKE_FUNC_NAME macro. It's pretty clear that you didn't read the whole answer in the FAQ: An equivalent circumlocution is necessary with the token-pasting operator ## when the values (rather than the names) of two macros are to be concatenated. -- char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[] ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p =b,i=24;for( +=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}} |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On 29 Jan, 23:15, Ben Pfaff <b...@cs.stanford.edu> wrote:
> dspfun <dsp...@hotmail.com> writes: > > On 29 Jan, 21:07, Ben Pfaff <b...@cs.stanford.edu> wrote: > >> dspfun <dsp...@hotmail.com> writes: > >> > #define PREPEND aa_ > >> > #define MAKE_FUNC_NAME(prepend, func_name) prepend ## func_name > > >> > void MAKE_FUNC_NAME(PREPEND, f1)(void) > >> > { > >> > printf("here..\n"); > >> > } > >> > ************************************************** ** > > >> > However, this does not define the function aa_f1, instead the function > >> > PREPENDf1 is defined. Why? Is there some way to get around this to > >> > achieve what is set out to be done? > > >> This is in the C FAQ: > > >> 11.17: I'm trying to use the ANSI "stringizing" preprocessing operator > >> `#' to insert the value of a symbolic constant into a message, > >> but it keeps stringizing the macro's name rather than its value. > > > It's almost the same, but not quite. What I am trying to do is to > > create the function name using macro substitution, however, the > > problem is that the PREPEND is never substituted for its corresponding > > #define inside the MAKE_FUNC_NAME macro. > > It's pretty clear that you didn't read the whole answer in the > FAQ: > > An equivalent circumlocution is necessary with the token-pasting > operator ## when the values (rather than the names) of two > macros are to be concatenated. > > -- > char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[] > ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p > =b,i=24;for( +=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+> 2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}- Dölj citerad text - > > - Visa citerad text - I saw that, my bad.. Thanks for your ! |
|
![]() |
| Outils de la discussion | |
|
|