|
|
| need help with declaring a 2d int array |
|
spellcaster
Member #1,493
September 2001
|
Quote: All preprocessor stuff is evil, including the header guards. Why is that? Quote: Not familiar with what you mean The # operator puts it's operand in quotes. #define DEBUG_CASE(value) case value: log(#value) enum { ERROR_SOMETHING }; switch (returnCode) { DEBUG_CASE( ERROR_SOMETHING ) // your code here break; } will evalute into: enum { ERROR_SOMETHING }; switch (returnCode) { case ERROR_SOMETHING: log("ERROR_SOMETHING"); // your code here break; } You can also use it to create string representations for your constants:
You can also use it if you're too lazy to write your own embedded language parser. I used the preprocessor to create a little "embedded language" so createing the JS interface is more easy. So, code like this:
GetterStart
GetterAttributesStart
Get( width )
// return width as JS var
Get( height )
// return height as JS var
GetterAttributesEnd
GetterEnd
Is translated into the correct interface calls:
The Image and IMG parts are of course dynamic So I didn't had to write a special parser. And writing interface code got more readable. -- |
|
23yrold3yrold
Member #1,134
March 2001
|
Quote: I got that idea from an artcile in Game Programming GemsI, where a complete state machine was developed that way. You should check it out, the resulting code is quite neat.
Not sure I curently have need for such a thing, but it's not like I can afford any of the GPG books anyway. -- |
|
ReyBrujo
Moderator
January 2001
|
spellcaster, it is useful, but when you create complex macros like in MFC, you are abusing the language features -- |
|
spellcaster
Member #1,493
September 2001
|
Quote: spellcaster, it is useful, but when you create complex macros like in MFC, you are abusing the language features
If it saves me several days of coding and testing, is more easy to read and doesn't require any new utilities, it's not abuse. I agree that inline functions and constants / enums should be used where it is possible. But if you want to output a linenumber, you should better use the _LINE_ macro Know your tools, and use them well. That includes cpp. -- |
|
ReyBrujo
Moderator
January 2001
|
Sure, but having something like this is evil: int main() { INITIALIZE_ALLEGRO(); PLAY_GAME(); TERMINATE_ALLEGRO(); } END_OF_MAIN()
-- |
|
X-G
Member #856
December 2000
|
No, making a game almost entirely out of templates is evil. -- |
|
Rash
Member #2,374
May 2002
|
spellcaster said: As long as there's no way to replace the # and ## macro operations, there's a need for defines. It's that easy. spellcaster, you're seriously pissing me off with this straw man. 95% of all instances of macros I ever saw at Allegro.cc were unjustified and then you bring up a few exotic uses of it where it might be useful supposedly as a rebuttal? I stand by the statement that macros should be minimized AS MUCH AS POSSIBLE and so should you. Frankly, you're not helping changing the general mindset here. |
|
X-G
Member #856
December 2000
|
Quote: spellcaster, you're seriously pissing me off with this straw man Who's strawmanning here, eh? Allegro's use of macros was not mentioned once prior to spellcaster saying that line, and he didn't mention it either. EDIT: For the record, Rash's post said "... I ever saw in Allegro ..." before he edited it. Not that it matters; the topic of "95% of instances of macros on Allegro.cc" wasn't brought up either. One broad statement was made, and a casual comment was made disfavoring that statement. Someone needs to lay off the alkaloids... -- |
|
Richard Phipps
Member #1,632
November 2001
|
I already mentioned MIN, MID, MAX are useful to me. So what's wrong with using them? |
|
23yrold3yrold
Member #1,134
March 2001
|
Quote: I already mentioned MIN, MID, MAX are useful to me. So what's wrong with using them?
Inlined versions are safer. Macros have uses, but very specialized ones. For the purposes of this discussion, in regards to the above code, macros are evil. -- |
|
Richard Phipps
Member #1,632
November 2001
|
These are the same as those defined by allegro? |
|
23yrold3yrold
Member #1,134
March 2001
|
The MIN() macro is pretty much the same everywhere ... #define min(X, Y) ((X) < (Y) ? (X) : (Y))
Makes me sick, it does. -- |
|
ReyBrujo
Moderator
January 2001
|
DJGPP has another, using the __typeof to prevent errors this macro has. -- |
|
Richard Phipps
Member #1,632
November 2001
|
looks ok to me. |
|
23yrold3yrold
Member #1,134
March 2001
|
a = MIN(--b, ++c); Depending on whether you're using the macro or an inlined function, you'll get two different results for 'a'. EDIT: The "expensive_computation" argument is another good one ... -- |
|
X-G
Member #856
December 2000
|
Quote: looks ok to me. min(expensive_computation(), another_expensive_computation()) ...after cpp... (expensive_computation() < another_expensive_computation() ? expensive_computation() : another_expensive_computation()) Just one example of why it's evil. Either way the "winner" is going to have to be executed twice. If either has side effects, you're even further up sh*t creek... -- |
|
ReyBrujo
Moderator
January 2001
|
int a = 9; int b = 10; int c = 0; c = MIN(a++, b); Should return 9, returns 10. -- |
|
Richard Phipps
Member #1,632
November 2001
|
ok.. it is 12:40am here. But what about normal variables: any problems here? |
|
23yrold3yrold
Member #1,134
March 2001
|
Quote: looks ok to me.
The fact this comment got thrice bum-rushed tells you this is a classic example of evil macros. Quote: any problems here? There? No. But wouldn't you prefer a function that NEVER gives you problems? See the first post; you can shoot yourself in the foot reall easy like ... -- |
|
Richard Phipps
Member #1,632
November 2001
|
Or... unsensible use of the macro. |
|
ReyBrujo
Moderator
January 2001
|
Should return 9, but returns 10. -- |
|
X-G
Member #856
December 2000
|
Quote: unsensible use of the macro Just by making it an inline function instead, this kind of misbehaviour is made impossible. You don't have to care any more, and the function will execute just as fast. Why give you the chance to shoot yourself in the foot if preventing it is just as powerful? -- |
|
23yrold3yrold
Member #1,134
March 2001
|
Quote: Or... unsensible use of the macro. Would you consider it better practice to make bulletproof code that always works, or code that relies on you not to screw it up every time you reference it? The macro has pitfalls, and zero advantages over the inlined template function. -- |
|
spellcaster
Member #1,493
September 2001
|
rash said: spellcaster, you're seriously pissing me off with this straw man. 95% of all instances of macros I ever saw at Allegro.cc were unjustified and then you bring up a few exotic uses of it where it might be useful supposedly as a rebuttal? I think I got that covered, eh? myself said: I agree that inline functions and constants / enums should be used where it is possible. I think you should apologize. And maybe you should read the posts you're replying to, and maybe even think twice before you post once. Just an idea. Anyway, saying that macros are "evil, evil, evil" is singleminded. And not very clever. Due to the idea that all a macro can do is just text replacement, nobody cares about the cpp anymore. Most don't even know about the # and ## operators and how to use them. But, from time to time macros are extremly useful, make your code more easy to read and more easy to maintain. unsigned char flags = BINARY2( 1001, 0011); is more easy to read and less error prone than the decimal or hexadecimal version of that number. assertmsg( value > 0, "The value must be > 0"); will give you a better error message than a pure assert(). Macros allow to create a string containing both file name and line number... which is quite neat, IMO. The point is: cpp is a tool. If you use it wisely, it'll help you. If you use it poorly, well, than you're not clever enough. Always use the right tool for the right job. Should one use a #define for constants or should one prefer enums or consts? In that case the define looses. But: Just because there are better ways for these two jobs doesn't mean that "macros are evil". And whoever telling you that macros are evil never read the cpp description -- |
|
Richard Phipps
Member #1,632
November 2001
|
<shrugs> I only use it as: c = MIN(a, b); anyway. And on that note, I'm off to bed. If I can sleep in this stifling heat.. |
|
|
|