Skip to content

Commit

Permalink
Avoid max() and min() clash with c++ stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Nov 13, 2019
1 parent 7c9e4f8 commit 30e4397
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions api/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ typedef enum {
#define SERIAL 0x0
#define DISPLAY 0x1

#ifndef min
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#endif

#ifndef max
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#endif

#ifndef constrain
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#endif
Expand Down Expand Up @@ -130,6 +116,33 @@ void loop(void);
} // extern "C"
#endif

#ifdef __cplusplus
template<class T, class L>
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
{
return (b < a) ? b : a;
}

template<class T, class L>
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
{
return (a < b) ? b : a;
}
#else
#ifndef min
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#endif
#ifndef max
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#endif
#endif

#ifdef __cplusplus

/* C++ prototypes */
Expand Down

0 comments on commit 30e4397

Please sign in to comment.