-
-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core library defines min, max, abs and friends as macros which may cause unexpected side effects #2069
Comments
These work correctly... |
I'm not sure of the state of the documentation when this issue was submitted, but right now it does warn you not to do that sort of thing. However this is C++; why not take advantage of it?
And if we want to compare 3 or more numbers:
(this was a random thought; don't pay much attention to it if you think it doesn't deserve it) |
A good discussion of each choice...
Do all Arduino programmers use C++? The code of interest from the link above...
|
Yes, they do. They may not know, but they do, since Arduino sketches are C++. |
I also like @cousteaulecommandant version. This would also enable multiple params for min() etc. |
Interesting. They propose using some C++ trickery involving
I don't know how good this is compared to my (In the link they explain that the PS: maybe this |
Never mind; it seems that my function cannot be used with different types either; so I'd prefer the one proposed by maniacbug on the forum (the one with Comparing different types can still be achieved by either explicitly specifying it as e.g. I'd still add So in short, the code for
(NB: I had a mistake in my original proposal; using By the way, did you know that Arduino doesn't seem to allow putting the |
Have more faith in the optimizer. While it may not be perfect, it is good enough that it eliminates trivial value copying. |
I do; it's just "the usual practice in C++". In fact I checked with and without Also the version with the & doesn't really affect the code size, so why not use it if it's "the C++ style" (and in fact the one |
Something to consider, as library writers are making more complex "topics" simpler through their library API, they will use stl (containers) which requires the min and max to be undef'ed; thus any user code after it will not have the macros available to them. Further, other library included after that use the min and max macros will now not compile. This mess needs to be cleaned up. As a library writer myself I am running into support issues due to other libraries undef'ing them. |
Suppose you pass an argument of a function call which has a side effect as an argument to abs() like this:
The abs() is defined in the Arduino.h as:
It is easy to see, that the function will be evaluated 2 times. Moreover, if a volatile variable is passed as an argument to such macro and the value of the variable gets changed by an interrupt handler, the result will also be unpredictable. What if the argument is a complex expression? Will the code optimization help to prevend a second evaluation of the expression? IMHO that is a good question to ask.
Using macro arguments in macro body more that 1 time is misleading and dangerous. In fact users of Arduino IDE has no way of looking into the source code to see a potential problem.
You should really review the folowing macros and either rewrite them as inline functions or document the side effects:
The text was updated successfully, but these errors were encountered: