#define Why does it not do what i want?

rifter1818

Well-known member
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++, youre basically defining a "macro function" - its only for inlining code. I guess they decided that wasnt a necessity with C#, the same way they left out the "inline" keyword for functions/methods.

-ner
 
In C# they deliberately didnt 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++)    //whats the value of i,j,k now ?
 
Last edited by a moderator:
PlausiblyDamp said:
In C# they deliberately didnt 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++)    //whats 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 isnt 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