libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.29k stars 904 forks source link

`minwindef.h` `min` and `max` macros conflicting with `std::numeric_limits` #957

Open nickbeth opened 10 months ago

nickbeth commented 10 months ago

Description

I'm using libcpr together with another library (glaze). In files where both are used (libcpr for requests and Glaze for JSON parsing of the response text), compilation is failing with the following error:

[...]/include\glaze/util/stoui64.hpp:14:54: error: too few arguments provided to function-like macro invocation
      return a <= std::numeric_limits<uint64_t>::max() - b;
                                                     ^
[...]\Windows Kits\10\Include\10.0.22621.0\shared\minwindef.h:193:9: note: macro 'max' defined here
#define max(a,b)            (((a) > (b)) ? (a) : (b))
        ^

If the two libraries are included in the following way, the above error appears:

#include <cpr/cpr.h>
#include <glaze/glaze.hpp>

By defining the NOMINMAX symbol before including the libcpr headers, the error disappears:

#define NOMINMAX
#include <cpr/cpr.h>
#include <glaze/glaze.hpp>

Inverting the include order also works:

#include <glaze/glaze.hpp>
#include <cpr/cpr.h>

Example/How to Reproduce

On Windows, include the libcpr headers before any code that uses std::numeric_limits::max() or std::numeric_limits::min().

Possible Fix

Define the NOMINMAX preprocessor symbol before including Windows headers in libcpr. As far as I've seen the min and max macros from minwindef.h are unused by libcpr.

Where did you get it from?

GitHub (branch e.g. master)

Additional Context/Your Environment

mpate132 commented 8 months ago

@lynxnb the solution was really helpful since it is not worth it to change every file in the cpr and append the line#define NOMINMAX before the #include <windows.h>, so maybe it's better if we can just define other macro on our own in our project.

Your solution really helped me. Thanks