ArduinoJson icon indicating copy to clipboard operation
ArduinoJson copied to clipboard

Much easier fix for min vs std::min issue

Open dberlin opened this issue 2 years ago • 3 comments

(It's not obvious where the website docs live in github, or i'd just submit a pull request) The troubleshooter for the std::min vs arduino min macro (https://arduinojson.org/v6/troubleshooter/?utm_source=github&utm_medium=readme#compiletime/macro-min/success) says to disable std::string/std::stream support.

Just to flag there is actually a much easier fix that does not require compiler flags and should work everywhere.

Before including ArduinoJson.h, do this:

#include <Arduino.h>
#undef min

min is guaranteed to be defined in C++ so we don't have to worry that we will remove the only definition (it's also supposed to be an inline function and not a macro, which is why it conflicts in the first place)

Including the header ourselves guarantees that all the macros are defined and that the header won't be reincluded by someone else. #undef min then removes the conflicting macro definition. This will work:

  1. On all systems where someone uses ArduinoJSON
  2. whether the min macro exists or not.
  3. without removing support for std::*

Figured i would flag this.

dberlin avatar Aug 03 '22 17:08 dberlin

Hi @dberlin,

Thank you for this feedback.

The source of the website is, indeed, in a private repo.

Is it sufficient to #undef min? or do we also need to #undef max?

Best regards, Benoit

bblanchon avatar Aug 04 '22 08:08 bblanchon

At least as you have things now, you would not have to #undef max, because ArduinoJson doesn't use max. If that changed, yes, you would run smack into the same problem unless you #undef max.

On Thu, Aug 4, 2022 at 4:27 AM Benoît Blanchon @.***> wrote:

Hi @dberlin https://github.com/dberlin,

Thank you for this feedback.

The source of the website is, indeed, in a private repo.

Is it sufficient to #undef min? or do we also need to #undef max?

Best regards, Benoit

— Reply to this email directly, view it on GitHub https://github.com/bblanchon/ArduinoJson/issues/1785#issuecomment-1204933170, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACPI2574CP3R2ZZDHUIAL3VXN5GNANCNFSM55PSVKWQ . You are receiving this because you were mentioned.Message ID: @.***>

dberlin avatar Oct 11 '22 08:10 dberlin

ArduinoJson doesn't use min either, but the STL uses it.

bblanchon avatar Oct 13 '22 07:10 bblanchon