Skip to content
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

warnings in msvc about mix/max macro while windows.h is used in the project #762

Closed
crea7or opened this issue Oct 1, 2017 · 6 comments
Closed
Assignees
Labels
platform: visual studio related to MSVC solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@crea7or
Copy link

crea7or commented Oct 1, 2017

In the version 2.1.1, msvc generates a few warnings about std::numeric_limits<>::max() (and min) usage. This is because windows.h have their own min/max macros definitions in the global namespace (here is microsoft kb article about it..
warning exact text: warning C4003: not enough actual parameters for macro 'max'

possible fixes:
1)

#define NOMINMAX

#ifdef max
#undef max
#endif

#ifdef min
#undef min
#endif
  1. use additional parenthesis

std::numeric_limits<uint8_t>::max() => (std::numeric_limits<uint8_t>::max)()

First two methods can produce problems, due to use original windows min/max in the gdi and more.
the third method should be tested on the other compilers.

Source to reproduce the problem:

#include "stdafx.h"
#include "windows.h"
#include "json.hpp"

using json = nlohmann::json;

int main( int argc, char const *argv[] )
{
	json j;
	j[ "test" ] = 1;
}
@nlohmann nlohmann added the platform: visual studio related to MSVC label Oct 1, 2017
@nlohmann
Copy link
Owner

nlohmann commented Oct 1, 2017

Does this also occur in the develop branch?

@crea7or
Copy link
Author

crea7or commented Oct 1, 2017

just tested dev branch - yes, even the errors appeared:

json.hpp(3583): warning C4003: not enough actual parameters for macro 'min'
json.hpp(3583): error C2589: '(': illegal token on right side of '::'
json.hpp(3583): error C2062: type 'unknown-type' unexpected
json.hpp(3583): error C2059: syntax error: ')'
json.hpp(3513): error C3615: constexpr function 'nlohmann::detail::operator ==' cannot result in a constant expression
json.hpp(3513): note: failure was because type 'nlohmann::detail::primitive_iterator_t' is not a literal type
...

changing line 3583 from:
difference_type m_it = std::numeric_limits<std::ptrdiff_t>::min();
to
difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
fixes all the problems.

@nlohmann
Copy link
Owner

nlohmann commented Oct 1, 2017

Thanks! I'll have a look.

@nlohmann nlohmann self-assigned this Oct 1, 2017
@nlohmann nlohmann added this to the Release 3.0.0 milestone Oct 1, 2017
nlohmann added a commit that referenced this issue Oct 2, 2017
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 2, 2017
@nlohmann
Copy link
Owner

nlohmann commented Oct 2, 2017

@crea7or Can you please check if this works now?

@crea7or
Copy link
Author

crea7or commented Oct 2, 2017

Everything is clear in visual studio 2015 + visual studio 2017 with all available sdk versions:
untitled-1

@crea7or crea7or closed this as completed Oct 2, 2017
@nlohmann
Copy link
Owner

nlohmann commented Oct 2, 2017

Thanks for checking!

nlohmann pushed a commit that referenced this issue Jul 30, 2022
* Fixed latest build error in msvc platform

warnings in msvc about mix/max macro while windows.h is used in the project #762

* Update for CI

None

* Update single_include/nlohmann/json.hpp

Co-authored-by: Florian Albrechtskirchinger <falbrechtskirchinger@gmail.com>

Co-authored-by: Florian Albrechtskirchinger <falbrechtskirchinger@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: visual studio related to MSVC solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants