Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   #define macro to enclose an older macro with strings (http://www.velocityreviews.com/forums/t284332-define-macro-to-enclose-an-older-macro-with-strings.html)

Dead RAM 07-13-2004 11:07 AM

#define macro to enclose an older macro with strings
 
Hey people, i'll try to keep this short ;)

Here is what I want to type (or at least close too)...

#define VER_BUILD 1
#define STR_VER_BUILD "VER_BUILD"

But what happends is the preprocessor see the quots in STR_VER_BUILD and
replaces that text with "VER_BUILD"...
I need it to see the VER_BUILD and replace it with 1, and only after doing
that replacement enclose the 1 in quots...
I tried using a number sign without any luck (number sign in a macro that
takes params encloses the next param in quots).

#define VER_BUILD 1
#define THING_TO_STR(thing) # thing
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)

Both these tries gave me the result of STR_VER_BUILD being replaced with
"VER_BUILD"... Instead of what I wanted... VER_BUILD being replaced with 1
and STR_VER_BUILD being replaced with "1"

Just so there isn't any confusion... any other place were i used VER_BUILD
alone, the preprocessor replaced that with 1.



John Harrison 07-13-2004 11:47 AM

Re: #define macro to enclose an older macro with strings
 

"Dead RAM" <deadram@hotmail.com> wrote in message
news:1oPIc.911822$Ar.646403@twister01.bloor.is.net .cable.rogers.com...
> Hey people, i'll try to keep this short ;)
>
> Here is what I want to type (or at least close too)...
>
> #define VER_BUILD 1
> #define STR_VER_BUILD "VER_BUILD"
>
> But what happends is the preprocessor see the quots in STR_VER_BUILD and
> replaces that text with "VER_BUILD"...
> I need it to see the VER_BUILD and replace it with 1, and only after doing
> that replacement enclose the 1 in quots...
> I tried using a number sign without any luck (number sign in a macro that
> takes params encloses the next param in quots).
>
> #define VER_BUILD 1
> #define THING_TO_STR(thing) # thing
> #define STR_VER_BUILD THING_TO_STR(VER_BUILD)
>
> Both these tries gave me the result of STR_VER_BUILD being replaced with
> "VER_BUILD"... Instead of what I wanted... VER_BUILD being replaced with 1
> and STR_VER_BUILD being replaced with "1"
>


This is a good example of the weirdness that is the C pre-processor. This
minor variation works

#define VER_BUILD 1
#define _THING_TO_STR(thing) # thing
#define THING_TO_STR(thing) _THING_TO_STR(thing)
#define STR_VER_BUILD THING_TO_STR(VER_BUILD)

Don't ask me to explain why because I don't know. It just a trick worth
knowing.

john



John Harrison 07-13-2004 11:54 AM

Re: #define macro to enclose an older macro with strings
 
>
> #define VER_BUILD 1
> #define _THING_TO_STR(thing) # thing
> #define THING_TO_STR(thing) _THING_TO_STR(thing)
> #define STR_VER_BUILD THING_TO_STR(VER_BUILD)
>


Before some else points it out, identifiers with a leading underscore
followed by uppercase letter are _NOT_ALLOWED_. Replace _THING_TO_STR with
something more suitable.

john



=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?= 07-13-2004 11:55 AM

Re: #define macro to enclose an older macro with strings
 
Dead RAM wrote:

> Hey people, i'll try to keep this short ;)
>
> Here is what I want to type (or at least close too)...
>
> #define VER_BUILD 1
> #define STR_VER_BUILD "VER_BUILD"
>
> But what happends is the preprocessor see the quots in STR_VER_BUILD and
> replaces that text with "VER_BUILD"...
> I need it to see the VER_BUILD and replace it with 1, and only after doing
> that replacement enclose the 1 in quots...
> I tried using a number sign without any luck (number sign in a macro that
> takes params encloses the next param in quots).
>
> #define VER_BUILD 1
> #define THING_TO_STR(thing) # thing
> #define STR_VER_BUILD THING_TO_STR(VER_BUILD)
>
> Both these tries gave me the result of STR_VER_BUILD being replaced with
> "VER_BUILD"... Instead of what I wanted... VER_BUILD being replaced with 1
> and STR_VER_BUILD being replaced with "1"
>
> Just so there isn't any confusion... any other place were i used VER_BUILD
> alone, the preprocessor replaced that with 1.


#include <stdio.h>
#include <stdlib.h>
#define STR_VER_BUILD "1"
#define VER_BUILD atoi(STR_VER_BUILD)
int main(void) {
printf("%d = \"%s\"\n", VER_BUILD, STR_VER_BUILD);
return 0;
}

Karl Heinz Buchegger 07-13-2004 12:22 PM

Re: #define macro to enclose an older macro with strings
 
hack_tick wrote:
>
> hi there
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:2li0ihFcpgj6U1@uni-berlin.de...
> [..]
> > Before some else points it out, identifiers with a leading underscore
> > followed by uppercase letter are _NOT_ALLOWED_. Replace _THING_TO_STR with
> > something more suitable.

>
> is it just the case with Macros or with All the identifiers ? is it Standard


All identifiers and yes it is Standard. Names starting with an underscore
followed by an uppercase letter are reserved for the compiler to aid in
implementing its own macros and templates without interfering with user
written source code.

--
Karl Heinz Buchegger
kbuchegg@gascad.at

hack_tick 07-13-2004 12:23 PM

Re: #define macro to enclose an older macro with strings
 
hi there
"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2li0ihFcpgj6U1@uni-berlin.de...
[..]
> Before some else points it out, identifiers with a leading underscore
> followed by uppercase letter are _NOT_ALLOWED_. Replace _THING_TO_STR with
> something more suitable.


is it just the case with Macros or with All the identifiers ? is it Standard
?



hack_tick 07-13-2004 12:25 PM

Re: #define macro to enclose an older macro with strings
 
BTW its working with VC6 for macros and identifiers(i tried with variable's
name only)



Karl Heinz Buchegger 07-13-2004 12:26 PM

Re: #define macro to enclose an older macro with strings
 
hack_tick wrote:
>
> BTW its working with VC6 for macros and identifiers(i tried with variable's
> name only)


Sure it works. This rule is more on the level of an (enforced) agreement.
There is nothing wrong with those names per se. But the compiler needs some names
(for macros, templates, ...) on it's own (eg. open the iostream header
file, if it exists on your implementation and see for yourself). That's
why the standard reserves such names for exclusive use of compiler writers only.
Just avoid such names and your macros will not interfere with something you
get unexpected by including some system header files.

--
Karl Heinz Buchegger
kbuchegg@gascad.at

Dead RAM 07-13-2004 01:31 PM

Re: #define macro to enclose an older macro with strings
 
I don't know how... or why... but ummm... 0.o thanks...

As a side note... I hate the guy who built the pre-processor... but love the
brain that caused this hack to show up ;)



JKop 07-13-2004 02:46 PM

Re: #define macro to enclose an older macro with strings
 
Dead RAM posted:


> #define VER_BUILD 1
> #define THING_TO_STR(thing) # thing
> #define STR_VER_BUILD THING_TO_STR(VER_BUILD)
>
> Both these tries gave me the result of STR_VER_BUILD being replaced
> with "VER_BUILD"... Instead of what I wanted... VER_BUILD being
> replaced with 1 and STR_VER_BUILD being replaced with "1"



This in some perverse way actually pleases me.


unsigned char const ver_build = 1;

const char* const str_ver_build = "1";


-JKop


All times are GMT. The time now is 07:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57