Skip to content

Commit 8ac887e

Browse files
committed
make min/max templates @hathach
1 parent 62e113f commit 8ac887e

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

cores/nRF5/Arduino.h

+27-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,33 @@ void suspendLoop(void);
8585
#undef abs
8686
#endif // abs
8787

88-
#define min(a,b) ((a)<(b)?(a):(b))
89-
#define max(a,b) ((a)>(b)?(a):(b))
88+
#ifdef __cplusplus
89+
template<class T, class L>
90+
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
91+
{
92+
return (b < a) ? b : a;
93+
}
94+
95+
template<class T, class L>
96+
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
97+
{
98+
return (a < b) ? b : a;
99+
}
100+
#else
101+
#ifndef min
102+
#define min(a,b) \
103+
({ __typeof__ (a) _a = (a); \
104+
__typeof__ (b) _b = (b); \
105+
_a < _b ? _a : _b; })
106+
#endif
107+
#ifndef max
108+
#define max(a,b) \
109+
({ __typeof__ (a) _a = (a); \
110+
__typeof__ (b) _b = (b); \
111+
_a > _b ? _a : _b; })
112+
#endif
113+
#endif
114+
90115
#define abs(x) ((x)>0?(x):-(x))
91116
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
92117
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

0 commit comments

Comments
 (0)