PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Prepend name to function names?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Prepend name to function names?

Réponse
 
LinkBack Outils de la discussion
Vieux 29/01/2008, 20h00   #1
dspfun
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Prepend name to function names?

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?
  Réponse avec citation
Vieux 29/01/2008, 20h07   #2
Ben Pfaff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Prepend name to function names?

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;}}}
  Réponse avec citation
Vieux 29/01/2008, 20h44   #3
dspfun
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Prepend name to function names?

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.

  Réponse avec citation
Vieux 29/01/2008, 20h55   #4
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Prepend name to function names?

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");
}
  Réponse avec citation
Vieux 29/01/2008, 21h12   #5
dspfun
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Prepend name to function names?

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?
  Réponse avec citation
Vieux 29/01/2008, 21h39   #6
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Prepend name to function names?

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."
  Réponse avec citation
Vieux 29/01/2008, 21h50   #7
dspfun
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Prepend name to function names?

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!
  Réponse avec citation
Vieux 29/01/2008, 22h15   #8
Ben Pfaff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Prepend name to function names?

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;}}}
  Réponse avec citation
Vieux 29/01/2008, 22h25   #9
dspfun
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Prepend name to function names?

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 !
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h14.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19875 seconds with 17 queries