Skip to content

Commit 30e4397

Browse files
committed
Avoid max() and min() clash with c++ stdlib
1 parent 7c9e4f8 commit 30e4397

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

Diff for: api/Common.h

+27-14
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@ typedef enum {
3737
#define SERIAL 0x0
3838
#define DISPLAY 0x1
3939

40-
#ifndef min
41-
#define min(a,b) \
42-
({ __typeof__ (a) _a = (a); \
43-
__typeof__ (b) _b = (b); \
44-
_a < _b ? _a : _b; })
45-
#endif
46-
47-
#ifndef max
48-
#define max(a,b) \
49-
({ __typeof__ (a) _a = (a); \
50-
__typeof__ (b) _b = (b); \
51-
_a > _b ? _a : _b; })
52-
#endif
53-
5440
#ifndef constrain
5541
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
5642
#endif
@@ -130,6 +116,33 @@ void loop(void);
130116
} // extern "C"
131117
#endif
132118

119+
#ifdef __cplusplus
120+
template<class T, class L>
121+
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
122+
{
123+
return (b < a) ? b : a;
124+
}
125+
126+
template<class T, class L>
127+
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
128+
{
129+
return (a < b) ? b : a;
130+
}
131+
#else
132+
#ifndef min
133+
#define min(a,b) \
134+
({ __typeof__ (a) _a = (a); \
135+
__typeof__ (b) _b = (b); \
136+
_a < _b ? _a : _b; })
137+
#endif
138+
#ifndef max
139+
#define max(a,b) \
140+
({ __typeof__ (a) _a = (a); \
141+
__typeof__ (b) _b = (b); \
142+
_a > _b ? _a : _b; })
143+
#endif
144+
#endif
145+
133146
#ifdef __cplusplus
134147

135148
/* C++ prototypes */

0 commit comments

Comments
 (0)