#define Why does it not do what i want?

rifter1818

Junior Contributor
Joined
Sep 11, 2003
Messages
255
Location
A cold dark place
in C++ i could just state say
#define Lerp(a,b,t)(a + ((b-a)*t))
and be on my way but this does not work quite so happily in c#, anyways i like my loosly defining simple equations (i mananged a close to 75% code reduction with perlin noise (to the point that i can now understand my code where as it took me a month to fully understand Ken Perlins code (although granted i didnt know c++ at the begining of that month). Anyways thanks in advance.
 
The #define in C++ did not carry over to C#. As far as I know, there is no equivalent. When using #define in C++, you're basically defining a "macro function" - it's only for inlining code. I guess they decided that wasn't a necessity with C#, the same way they left out the "inline" keyword for functions/methods.

-ner
 
In C# they deliberately didn't allow #define macros on the grounds that they are not typesafe and can introduce subtle errors that way. Also they can have unexpected side effects for the un-wary - classic example being
Code:
#define max (a,b)   (((a)>(b))?(a):(b))

int i=1,j=2;

int k = max(i++,j++)    //what's the value of i,j,k now ?
 
Last edited:
PlausiblyDamp said:
In C# they deliberately didn't allow #define macros on the grounds that they are not typesafe and can introduce subtle errors that way. Also they can have unexpected side effects for the un-wary - classic example being
Code:
#define max (a,b)   (((a)>(b))?(a):(b))

int i=1,j=2;

int k = max(i++,b++)    //what's the value of i,j,k now ?
well darn i liked not having to strongly type every variable... oh well its not that big of a deal, and shouldnt k = 2, i = 2,b = 3 after the abouve mentioned code as arnt the ++ operaters supposed to be acted opon after the function? example while(cin >> Array[Index++]){} well fair enough thats not actually a function but you get my point. anyways thanks for your input you two :D
 
Problem is a define isn't a function but a literal text substitution, one of the arguments would have the ++ operator invoked twice the other once.

Code:
#define max (a,b)   (((a)>(b))?(a):(b))

k= max(i++,j++)   ;
//above expands to
k = (((i++)>(j++))?(i++):(j++))   //spot the error?
 
Back
Top