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.
The MIN Challenge
11–20 of 27 posts
Re: The MIN Challenge
#12Not 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.
Re: The MIN Challenge
#13Not 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.
Re: The MIN Challenge
#14Yes, 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.
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
#15Not 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.
min(1, 2, out);
return out;
to be condensed into one line.Re: The MIN Challenge
#16Not 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 (failure) {
errno = EINVAL;
return -1;
}
into if (failure)
return (errno = EINVAL, -1);Re: The MIN Challenge
#17Macros 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…
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
#18Can 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
#19 auto min = [] (auto a, auto b) { return a Re: The MIN Challenge
#20Meanwhile, in C++14: auto min = [] (auto a, auto b) { return a