Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • Administrators
Posted (edited)

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

#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 ?

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
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

#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

  • Administrators
Posted

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.

 

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

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...