![]() |
|
need help with declaring a 2d int array |
Rash
Member #2,374
May 2002
![]() |
X-G said: EDIT: For the record, Rash's post said "... I ever saw in Allegro ..." before he edited it. You must be imagining things, because I did no such thing. Ten to one this thread will now degenerate into a C vs C++ vs C99 discussion...Oh wait! |
23yrold3yrold
Member #1,134
March 2001
![]() |
Quote: All it means that you shouldn't use macros for constants and inline methods. It's as easy as that.
Right. But that's what most people use them for, like it or not. Like, you know, above. William Labbett: MACROS AER TEH EVAL!!1 RUNRUNRUNRUN ... -- |
spellcaster
Member #1,493
September 2001
![]() |
Quote: Teach the acolyte that macros are evil. That's not needed. Teach everybody that he should use consts and enums for constant values, and inline functions for inline functions. That's it. No black magic involved here. EDIT: -- |
Jonatan Hedborg
Member #4,886
July 2004
![]() |
Here's a great opportunity to mold a young mind... How would an inlined template-using MIN look? (i have no idea how templates work, but im sure it can be useful
|
X-G
Member #856
December 2000
![]() |
I assume something like: inline template <typename T> T min(T a, T b) { return (a < b ? a : b); } EDIT: Forgot return type. -- |
23yrold3yrold
Member #1,134
March 2001
![]() |
Off the top of my head ... template<class T> inline T cadd(T x, T y) { return (x < y ? x : y); } I might have goofed; it's untested ... EDIT: All fixed. -- |
Rash
Member #2,374
May 2002
![]() |
Ok, to stay in character I'll make my 1000th post useful: #include <algorithm> std::min(a,b);
[EDIT] |
Jonatan Hedborg
Member #4,886
July 2004
![]() |
X-G: Your code didnt compile.
|
23yrold3yrold
Member #1,134
March 2001
![]() |
Quote: No need to reinvent the wheel, is there?
No need to include a standard header for four lines, either. Quote: 23: your code worked after i replaced X with z1 and Y with z2
Whoops; right. -- |
X-G
Member #856
December 2000
![]() |
Quote: X-G: Your code didnt compile.
How about telling me WHY? -- |
23yrold3yrold
Member #1,134
March 2001
![]() |
I think it's the inline placement, X-G ... -- |
Jonatan Hedborg
Member #4,886
July 2004
![]() |
I dont know WHY, but im sure this can help
|
spellcaster
Member #1,493
September 2001
![]() |
Rash: I think my point is proven then, eh? -- |
X-G
Member #856
December 2000
![]() |
Quote: main.cpp:5: error: syntax error before `<' token
Err, excuse me? That's where "template" is at. Well, considering how vague errors can be some times, let's just assume it's the placement of the word "inline". -- |
Jonatan Hedborg
Member #4,886
July 2004
![]() |
yeah, worked when i moved it.. (template <typename T> inline T min(T a, T b) { return (a < b ? a : b); })
|
Rash
Member #2,374
May 2002
![]() |
Make the return value a reference and you'll have nailed it. spellcaster: http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.14 |
23yrold3yrold
Member #1,134
March 2001
![]() |
Quote: Make the return value a reference and you'll have nailed it. Return a reference? I can see the parameters as references, but by constructing the object right on the return statement ... well, I didn't know you could beat that. I might be wrong, but what are we returning a reference to? BTW, that is the coolest new entry into the C++ FAQ Lite ever. -- |
Rash
Member #2,374
May 2002
![]() |
Well in that case make the parameters references as well. The declaration of std::min, after all, is: template <class T> const T& min(const T& a, const T& b);
|
spellcaster
Member #1,493
September 2001
![]() |
rash: what's your point? You even quoted me saying that there are uses for macros. Then you replied that this "pisses you off". While I can understand the latter, you've still been rude -- |
Kitty Cat
Member #2,815
October 2002
![]() |
Wouldn't it be hard to create an inline min() that handles floats, doubles, chars, and ints, without using C++? That's one of the problems I had with my game, I had inline min()/max() functions, but they were messing up because it took ints, but I was passing floats (though I wasn't initially aware of this). I changed them to defines like Allegro has and now it works hunky-dory. Though I do kinda wish the inline functions worked.. branchless min/max is a good thing to have. -- |
ReyBrujo
Moderator
January 2001
![]() |
KC, you could send a third parameter stating the type of the parameters you are sending. As for creating one, this is how DJGPP defines it: #define min(X, Y) \ ({ typeof (X) __x = (X), __y = (Y); (__x < __y) ? __x : __y })
-- |
Johan Halmén
Member #1,550
September 2001
|
You know you use too much macros, when you demand syntax hilightning in them. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
|
|