#DEFINE "magic"...

Started by
14 comments, last by Erzengeldeslichtes 19 years, 11 months ago
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."?
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Advertisement
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)
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]
You should never let your fears become the boundaries of your dreams.
#define blah(name)   printf(#name);  


As my knowledge of C++ console io I went fro printf.. But I guess it works with cout to..
[edit] - 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]
<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
"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
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]
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
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]
"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
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"
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]
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
I don''t understand why you need to use the preprocessor, when this does the same thing...

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

This topic is closed to new replies.

Advertisement