]> git.ozlabs.org Git - ccan/blobdiff - ccan/minmax/_info
minmax: New module, safe min and max macros
[ccan] / ccan / minmax / _info
diff --git a/ccan/minmax/_info b/ccan/minmax/_info
new file mode 100644 (file)
index 0000000..1490ba4
--- /dev/null
@@ -0,0 +1,45 @@
+#include <string.h>
+#include "config.h"
+
+/**
+ * minmax - typesafe minimum and maximum functions
+ *
+ * The classic implementation of minimum / maximum macros in C can be
+ * very dangerous.  If the two arguments have different sizes, or
+ * different signedness, type promotion rules can lead to very
+ * surprising results.
+ *
+ * This module implements typesafe versions, which will generate a
+ * compile time error, if the arguments have different types.
+ *
+ * Example:
+ *     #include <ccan/minmax/minmax.h>
+ *     #include <stdio.h>
+ *
+ *     int main(int argc, char *argv[])
+ *     {
+ *             printf("Signed max: %d\n", max(1, -1));
+ *             printf("Unsigned max: %u\n", max(1U, -1U));
+ *             return 0;
+ *     }
+ *
+ * Author: David Gibson <david@gibson.dropbear.id.au>
+ * License:  CC0 (Public domain)
+ *
+ * Ccanlint:
+ *      // We need several gcc extensions
+ *      tests_compile_without_features FAIL
+ */
+int main(int argc, char *argv[])
+{
+       /* Expect exactly one argument */
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               printf("ccan/build_assert\n");
+               return 0;
+       }
+
+       return 1;
+}