Skip to content

Commit c4a2705

Browse files
author
Owen L - SFE
committed
add compatibility for standard C++ min/max
1 parent fa37954 commit c4a2705

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

cores/arduino/ard_sup/Arduino_defines.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,33 @@
2828
#undef abs
2929
#endif // abs
3030

31-
#define min(a, b) ((a) < (b) ? (a) : (b))
32-
#define max(a, b) ((a) > (b) ? (a) : (b))
31+
#ifdef __cplusplus
32+
template<class T, class L>
33+
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
34+
{
35+
return (b < a) ? b : a;
36+
}
37+
38+
template<class T, class L>
39+
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
40+
{
41+
return (a < b) ? b : a;
42+
}
43+
#else
44+
#ifndef min
45+
#define min(a,b) \
46+
({ __typeof__ (a) _a = (a); \
47+
__typeof__ (b) _b = (b); \
48+
_a < _b ? _a : _b; })
49+
#endif
50+
#ifndef max
51+
#define max(a,b) \
52+
({ __typeof__ (a) _a = (a); \
53+
__typeof__ (b) _b = (b); \
54+
_a > _b ? _a : _b; })
55+
#endif
56+
#endif
57+
3358
#define abs(x) ((x) > 0 ? (x) : -(x))
3459
#define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
3560
#define round(x) ((x) >= 0 ? (long)((x) + 0.5) : (long)((x)-0.5))

0 commit comments

Comments
 (0)