Greatest element of a list: Difference between revisions
Content deleted Content added
→{{header|C}}: variadic macro to handle different types with a single macro |
|||
Line 65: | Line 65: | ||
return themax; |
return themax; |
||
}</C> |
}</C> |
||
The following macro can be used with any number and type of arguments, provided that the arguments are ''simple'', i.e. must not contain subexpressions where commas appear (this is because of the way the arguments are counted; the macro can be modified so that it is up to the caller to count the number of arguments passed). |
|||
{{works with|GCC}} |
|||
<c>#include <stdarg.h> |
|||
#define MAX(A,...) ({ inline __typeof__ (A) _max_(__typeof__ (A) a, ...) {\ |
|||
va_list l; int i,c; char *s = #__VA_ARGS__; __typeof__ (A) max = a;\ |
|||
__typeof__ (A) t;\ |
|||
for(c=1;*s!=0;s++) if (*s==',') c++;\ |
|||
va_start(l, a);\ |
|||
for(i=0;i<=c;i++) {\ |
|||
if ((t=va_arg(l,__typeof__ (A))) > max) max = t;\ |
|||
}\ |
|||
va_end(l); return max;\ |
|||
}\ |
|||
_max_((A),__VA_ARGS__);\ |
|||
})</c> |
|||
=={{header|C++}}== |
=={{header|C++}}== |