Live data from Hacker News

The MIN Challenge

rus.har.mn

11–20 of 27 posts

Re: The MIN Challenge

#11
post #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.

It simplifies the grammar, immensely.

Re: The MIN Challenge

#12
post #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.

[deleted]

Re: The MIN Challenge

#13
post #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.

I know this mainly as a useful macro implementation trick if where you want to be able to use it in an expression.

Re: The MIN Challenge

#14

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.

Yep, but for min the C11 generics would work just as well and allow for a bit more flexibility than the older approaches.

Though you do have to name mangle manually and they aren't quite as powerful as you'd probably want.

http://www.robertgamble.net/2012/01/c11-generic-selections.h...

Re: The MIN Challenge

#15
post #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.

If the min macro would have worked, it would have allowed the two lines

    min(1, 2, out);
    return out;
to be condensed into one line.

Re: The MIN Challenge

#16
post #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.

It may not be a good reason to do this, but you can condense

    if (failure) {
        errno = EINVAL;
        return -1;
    }
into

    if (failure)
        return (errno = EINVAL, -1);

Re: The MIN Challenge

#17

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…

"min" is a pathological case for a macro because it isn't what macros were meant for.

Tell that to Kernighan and Ritchie. The second macro in K&R (I googled a PDF of the second edition) is

  #define  max(A, B)  ((A) > (B) ? (A) : (B))
Yes, that's lowercase 'max', and they even mention "This macro will work for any data type; there is no need for different kinds of max for different data types, as there would be with functions"

Macros certainly were intended for this, as it was the only way to guarantee that the compiler inlined code (probably the only way it ever inlined function calls)

And for the curious: the first macro they give is

  #define  forever  for(;;)
That certainly is what macros were meant for :-), and doesn't even save on typing.

But yes, in modern C, nobody would use a macro for this.

Re: The MIN Challenge

#18

Can anyone explain the advantage of macros like this, rather than just using an `inline function`? Is it because C doesn't have generics?

In older C, people tended to be unwilling to trust the compiler's ability to inline functions, so there was a lot of use of macros to avoid small repeated function calls.
Post reply on HN