mcci-catena / Arduino_Core_STM32

STM32 core support for Arduino
2 stars 7 forks source link

Use of <algorithm> for min/max not compatible with many libraries #184

Closed terrillmoore closed 2 years ago

terrillmoore commented 2 years ago

The STM32 BSP uses the following code for min/max:

In cores\arduino\wiring_constants.h, lines 24 ff:

#ifdef __cplusplus
#include <algorithm>
using std::min;
using std::max;
#else // C
...

Unfortunately, with the version of the compiler and libraries that we use, this causes things like the Adafruit ePaper Display Library not to compile, with cryptic warnings related to iterators pointing at uses of min and max. Comparing to other BSPs, I found that the Arduino.cc H7 BSP provides its own implementation:

In cores\arduino\api\Common.h, lines 122 ff:

#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
...

This (and the Adafruit/MCCI SAMD BSP, which just #define min() and max()) compile the Adafruit libraries without difficulty.

Patching the STM32 BSP with the code from the H7 BSP solves the problem.