2 ms·
It used to be #define min(x,y) ((x) < (y) ? (x) : (y))
by Animats 11y ago
It used to be
#define min(x,y) ((x) < (y) ? (x) : (y))
- pjmlp 11y agoWhich is anything but safe.
- azinman2 11y agoCan you explain more please?
- copascetic 11y agoBecause a macro in C/C++ is just text expansion, the given implementation will evaluate its arguments more than once, which will not be obvious at the "call" site and could have unintended consequences if the expressions have side-effects.
- azinman2 11y agoIs there another way to do it as a macro that avoids this? How do you have temporary variables potentially in the middle of a larger expression, e.g. if (min(a,b) == 3) {...}
- vbernat 11y agoBetter use: #define min(X, Y) \ ({ typeof (X) x_ = (X); \ typeof (Y) y_ = (Y); \ (x_ < y_) ? x_ : y_; })