Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > #define macro to enclose an older macro with strings

Reply
Thread Tools

#define macro to enclose an older macro with strings

 
 
Dead RAM
Guest
Posts: n/a
 
      07-13-2004
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.


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-13-2004

"Dead RAM" <(E-Mail Removed)> wrote in message
news:1oPIc.911822$(E-Mail Removed) .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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-13-2004
>
> #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


 
Reply With Quote
 
=?UTF-8?B?IkRhcmlvIChkcmlua2luZyBjb++sgGVlIGluIHRoZSBv76yDY2XigKYpIg==?=
Guest
Posts: n/a
 
      07-13-2004
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;
}
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-13-2004
hack_tick wrote:
>
> hi there
> "John Harrison" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> [..]
> > 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
hack_tick
Guest
Posts: n/a
 
      07-13-2004
hi there
"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
[..]
> 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
?


 
Reply With Quote
 
hack_tick
Guest
Posts: n/a
 
      07-13-2004
BTW its working with VC6 for macros and identifiers(i tried with variable's
name only)


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-13-2004
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
(E-Mail Removed)
 
Reply With Quote
 
Dead RAM
Guest
Posts: n/a
 
      07-13-2004
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


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-13-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
enclose a negative number in columns puzzlecracker C++ 9 12-22-2008 03:23 PM
Regexp to enclose text with P-tag Andreas N Perl 1 05-18-2004 01:43 PM
Regexp to enclose text with P-tag SubZane Python 1 05-18-2004 01:09 PM
Regexp to enclose text with P-tag Andreas N Python 0 05-18-2004 01:05 PM
[Namespace] Could I enclose different namespaces ? Cram TeXeD XML 0 04-06-2004 06:41 AM



Advertisments