Macro and Member Function Conflict

I have a problem: std :: numeric_limits :: min () conflicts with the macro "min" defined in "windef.h". Is there any way to resolve this conflict without an indefinite "min" macro. The link below provides some hints, however I was not able to use parentheses with a static member function.

What are some tricks I can use with macros?

Thanks in advance.

+17
c ++
Sep 08 '09 at 13:46
source share
6 answers

The workaround is to use the brackets: int max = (numeric_limits<int>::max)();

It allows you to include windef.h , does not require you to #undef max (which can have adverse side effects), and there is no need for #define NONIMAX . It works like a charm!

+29
Aug 23 '11 at 19:05
source share
— -

The only really general solution is to not include windows.h in the headers .

This header is a killer and does everything it can to make your code explode. It will not compile without the MSVC language extension extension, and this is the worst example of macro abuse I have ever seen.

Include it in a single .cpp file, and then put the wrappers in a header that the rest of your code can use. If windows.h is not displayed, it cannot conflict with your names.

For the min / max case, you can #define NOMINMAX before enabling windows.h. Then it will not define these specific macros.

+23
Sep 08 '09 at 2:00
source share

Yes, I am facing the same problem. I found only one solution:

 #ifdef min #undef min #endif //min 

Place it right after being turned on.

+1
Sep 08 '09 at 13:48
source share

In addition to the jalf answer, you can also #define WINDOWS_LEAN_AND_MEAN before enabling windows.h. It will get rid of the minimum, maximum and additional noise from the window headers.

+1
Sep 08 '09 at 14:34
source share

Dewfy, The problem with this solution is if you want to use macros after closing.

I even tried to detect NOMINMAX, but it did not work.

The best solution I found was what Johannes Schaub had: std :: numeric_limits :: min) ()

+1
Feb 04 '10 at 13:32
source share
 #define NOTHING int main() { int m = -1; m = max( 0, 1 ); // expands max macro m = max NOTHING ( 0, 1 ); // error 'max': identifier not found // functions-like macros are defined as an // identifier immediately followed by a left // parenthesis - N3690/16.3/10 m = std::max NOTHING ( 2, 3 ); // NOTHING stops the macro // expansion (max is not followed by left // parenthesis): std::max is called m = (std::max)( 4, 5 ); // the enclosing parenthesis stops the macro // expansion (max is not followed by left // parenthesis): std::max is called return 0; } 
0
Nov 25 '16 at 18:55
source share



All Articles