-->
Page 1 of 2

Arduino Min/Max

PostPosted: Fri Aug 14, 2015 3:36 pm
by martinayotte
I had a case where I need to call min() function, I've simply got the following error : :?
So, I used the suggested alternative std::min() ...

Code: Select allSketch_Buffet.ino:810:57: error: 'min' was not declared in this scope
Sketch_Buffet.ino:810:57: note: suggested alternative:
In file included from /home/martin/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9/xtensa-lx106-elf/include/c++/4.8.2/memory:62:0,
                 from /home/martin/.arduino15/packages/esp8266/hardware/esp8266/1.6.5-947-g39819f0/cores/esp8266/FS.h:25,
                 from Sketch_Buffet.ino:6:
/home/martin/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9/xtensa-lx106-elf/include/c++/4.8.2/bits/stl_algobase.h:239:5: note:   'std::min'
     min(const _Tp& __a, const _Tp& __b, _Compare __comp)

Re: Arduino Min/Max

PostPosted: Fri Aug 14, 2015 4:40 pm
by lethe
You need to include <algorithm> to use std::min, see http://en.cppreference.com/w/cpp/algorithm/min
Of course you can also define a simple macro:
Code: Select all#define min(X, Y) (((X)<(Y))?(X):(Y))

Be careful though, if you pass something like x++ to the macro, the statement might be evaluated more than once and x ends up being incremented twice.

Re: Arduino Min/Max

PostPosted: Fri Aug 14, 2015 6:36 pm
by martinayotte
I mean, normally on Arduino, such as Uno or Nano, we don't need to include any thing and min()/max() are already defined.
Here, on ESP, I had to specify the "std::" namespace (I didn't have to add a include), and it worked !
So, it is a small glitch, but if someone is porting a existing library, he will probably faced that ...

EDIT: I didn't have to add the include simply because it was already included indirectly.

Re: Arduino Min/Max

PostPosted: Wed Oct 21, 2015 9:02 am
by Asetyde
Thx , i not find a why ...

But is not strange things ?