Live data from Hacker News

The MIN Challenge

rus.har.mn

1–10 of 27 posts

Re: The MIN Challenge

#5
Yes, C doesn't have generics. In practice most C code uses the fourth version, and you just have to remember not to put things with side effects as macro arguments.

#define MIN(a, b) ((a) C11 does introduce very limited type-variant macros.

Re: The MIN Challenge

#6
Macros 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 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

#7

Macros 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…

exactly my attitude towards them - modern compilers will generally optimise simple functions like "min" so that they're just as efficient as macro equivalents.

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

#9

Macros 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…

In my experience, some compilers will inline functions when it is an optimization (particularly if you declare them static inline).

Re: The MIN Challenge

#10
Not entirely related to the post, but what's the sane use case for doing:

  return (expr, expr)
like in the first example? I cannot think of a good reason to do this.
Post reply on HN