#### Archived

This topic is now archived and is closed to further replies.

# #DEFINE "magic"...

This topic is 5175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Yes, yes, macros are evil, but they are also the lesser evil in this case. What I have is a derivitive creator that takes the name you want and generates the header that you need for it, which you then modify with a CPP. Now the problem is that I need to be able to take what is given to me straight from a define, and make it into a string. Now, I can see much confusion from that statement, so here''s what I want:
#define BLAH(name) cout << "name";

See what I mean? Let''s say I have a variable named foo which holds a string "bar". Now if I do BLAH(foo), I want it to print out foo, not bar. Is it possible? When I tried with the source I put above it printed out name, not foo and not bar (and I understand why, I mean, how''s the compiler supposed to know wether I want that "name" replaced or if it''s actually a part of the text?). So is there a way to tell it "Yes, I do want that name replaced."?

##### Share on other sites
Yes, it''s possible, using a # prefix:
#define BLAH(name) cout << #name

If the thing you want to convert to a string is the result of another macro you can use the BOOST_PP_STRINGIZE macro from the boost preprocessor library (boost.org)

##### Share on other sites
This is will print: foo = bar
#define VAR_DUMP( var )	cout << #var " = " << var << "\n"

edit: bah,... beaten by twanvl

You should never let your fears become the boundaries of your dreams.

[edited by - _DarkWIng_ on May 20, 2004 4:26:28 PM]

##### Share on other sites
#define blah(name)   printf(#name);

As my knowledge of C++ console io I went fro printf.. But I guess it works with cout to..
 - Slow connection... Got beaten not one..but twice.. Horrible...
"For every complex problem there is an answer that is clear, simple, and wrong." H L Mencken

[edited by - Rickmeister on May 20, 2004 4:27:25 PM]

##### Share on other sites
<joke>
#define MAGIC MORE_MAGIC
</joke>

Sorry...

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

##### Share on other sites
Ah, thanks for that. Out of curiosity, does that work with TCHAR or am I going to have to rely on the converter from ansi to unicode?

Edit:
twanvl said:
If the thing you want to convert to a string is the result of another macro you can use the BOOST_PP_STRINGIZE macro from the boost preprocessor library (boost.org)

So if I want to do, say #(name ## _Combined) or something like that I'm going to have to use boost?

[edited by - Erzengeldeslichtes on May 20, 2004 4:38:54 PM]

##### Share on other sites
quote:
Original post by Erzengeldeslichtes
Ah, thanks for that. Out of curiosity, does that work with TCHAR or am I going to have to rely on the converter from ansi to unicode?

std::cout works with narrow characters (char). std::wcout works with wide characters (wchar_t).

Somewhat important note - wide characters != Unicode. Unicode is only one particular encoding, which does use wide characters. Which encoding is actually used by a stream is controlled by whatever locale the stream has been imbued with.

As for the stringizing operator, I do believe it only yields narrow strings. You'll have to prefix with an L by yourself.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

[edited by - Fruny on May 20, 2004 4:41:51 PM]

##### Share on other sites
quote:

So if I want to do, say #(name ## _Combined) or something like that I''m going to have to use boost?

If you want to do it that way, probably yes (don''t know for sure) you might end up with "name ## _Combined" or probably a compile error. But you can do this in another way:
#name "_Combined"
In C(++) if you write "one string" "another string" they will be concatenated to form "one stringanother string"

##### Share on other sites
And now that I know about that, I can do "#define Blah(name, prefix) class prefix ## _SPECIAL_ ## name { public: char* Name() { return #prefix #name; } };", which is a simplified version of what I want, and now works! Thank you again!

Sure, preprocessor can work in strange ways, but I like it's capabilities... Just have to make sure it's used correctly.

[edited by - Erzengeldeslichtes on May 20, 2004 5:09:04 PM]

##### Share on other sites
I don''t understand why you need to use the preprocessor, when this does the same thing...

void BLAH(char* name){    cout << name;}

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002295
×