The MIN Challenge
rus.har.mn
The MIN Challenge
1–10 of 27 posts
Re: The MIN Challenge
#2Is it because C doesn't have generics?
Re: The MIN Challenge
#3Can anyone explain the advantage of macros like this, rather than just using an `inline function`? Is it because C doesn't have generics?
Re: The MIN Challenge
#4Can anyone explain the advantage of macros like this, rather than just using an `inline function`? Is it because C doesn't have generics?
Re: The MIN Challenge
#5#define MIN(a, b) ((a) C11 does introduce very limited type-variant macros.
Re: The MIN Challenge
#6In reality "min" should just be a function, and you should write a separate function for each type you want to define it over. This is how this problem is solved in C, and almost all other languages. It just looks deceptive because C's native operators like "" happen to be generic across some number types. This is just for convenience, it isn't how C is meant to be programmed. Pretty much every other language also makes you define different implementations of functions when you have a different type you want it to work on. Lots let you type the same function name, independent of the type - via generics, interfaces, or type-classes - but all call out to different implementations at the end of the day. Very few can derive those implementations.
As far as I am concerned, Macros in C are best used just to save on typing. They get a lot of hate because the syntax looks like functions - so people try to use them as functions. I don't recommend using them as functions, instead use them to save typing - because saving typing and avoiding repeating yourself can only reduce errors.
Re: The MIN Challenge
#7Macros in C can sometimes be nasty, but "min" is a pathological case for a macro because it isn't what macros were meant for. In reality "min" should just be a function, and you should write a separate function for each type you want to define it over. This is how this problem is solved in C, and almost all other languages. It just looks deceptive because C's native operators like " " happen to be generic across some…
For example, for a project that I did, I needed specific access methods for each member of a struct, and for each method to be written with a separate function. With macros, it was as easy as defining a macro to generate said function (given the member name), and then calling said macro for each member to generate the code.
Re: The MIN Challenge
#8Can anyone explain the advantage of macros like this, rather than just using an `inline function`? Is it because C doesn't have generics?
Re: The MIN Challenge
#9Macros in C can sometimes be nasty, but "min" is a pathological case for a macro because it isn't what macros were meant for. In reality "min" should just be a function, and you should write a separate function for each type you want to define it over. This is how this problem is solved in C, and almost all other languages. It just looks deceptive because C's native operators like " " happen to be generic across some…
Re: The MIN Challenge
#10 return (expr, expr)
like in the first example? I cannot think of a good reason to do this.