How do I call std::min
when min
has already been defined as a macro?
5 Answers
(std::min)(x,y)
The parentheses around min
prevent macro expansion. This works with all function macros.
-
6@André: Yes. Calling a function like this is given as an example in
[n3290: 3.4.2]
, and it's clear that this would prevent macro expansion (6.10.3/10
in C99; I'm sure the wording is nearby in C90.) Sep 16, 2011 at 20:00 -
Just out of pure curiosity, is there a way to prevent the expansion of a simple
#define foo 5
macro, or is this override behavior only for functions? Sep 16, 2011 at 20:02
On Windows, you need to define NOMINMAX
before including any windows headers, preferable at the beginning of precompiled header.
-
-
5@Mehrdad: including Windows headers is already non-portable as it stands. Sep 16, 2011 at 19:54
-
@Andre: I agree, that's precisely why I said I still like it.. :P Sep 16, 2011 at 19:55
-
3Can also use /D NOMINMAX command-line option in the project file. This is a useful alternative if you don't have PCH's.– MSaltersSep 17, 2011 at 23:34
Use #undef min
in your code, after #include <>
directives.
#include <...> // bad header that defines `min` macro
#ifdef min
#undef min
#endif
// rest f code.
Addendum: If you need to keep the value of the min
macro afterwards, you can disable its definition temporarily using a non-portable solution on some compilers. For instance, Microsoft's C++ compiler has a push_macro
pragma that also seems to be supported by GCC.
-
Er, I can't really do that because that messes up everything afterward that could depend on the macro for some reason... it's a rather poor hack, not a real solution. :\ Sep 16, 2011 at 19:48
-
2@Mehrdad:
min
as a macro is a hack to begin with. Best to avoid it. If you really want a macro, use one namedMIN
or something else that does not collide.– hammarSep 16, 2011 at 19:50 -
1@Mehrdad: posted a solution for temporary disabling instead. However, there's no portable solution as the language clearly states that if a macro is defined, then the textual replacement is done by the pre-processor and appears replaced to the compiler. There's no C++ language construct that can be used to distinguish
min
the function andmin
the macro. Sep 16, 2011 at 19:52 -
@hammar: The trouble is that you're assuming that this is in a C++ file. If this is a header file, it'll screw up whoever includes it, because they don't expect this to happen. (Edit: For the temporary disabling, I guess that works, but I was hoping for a better solution..) Sep 16, 2011 at 19:52
You might be able to avoid the macro definition by:
#undef
- avoid the definition in the first place (either by configuration such as
#define NOMINMAX
or similar or avoiding including the offending header)
If those options can't be used or you don't want to use them, you can always avoid invoking a function-like macro with an appropriate use of parens:
#include <algorithm>
#include <stdio.h>
#define min(x,y) (((x) < (y)) ? (x) : (y))
int main()
{
printf( "min is %d\n", (std::min)( 3, 5)); // note: the macro version of `min` is avoided
}
This is portable and has worked since the dark, early days of C.
I found a couple of other ways to do it:
Method 1:
using std::min;
min(a, b); // uses either the macro or the function (don't add side effects!)
Method 2:
#ifndef BOOST_PREVENT_MACRO_SUBSTITUTION
#define BOOST_PREVENT_MACRO_SUBSTITUTION
#endif
...
std::min BOOST_PREVENT_MACRO_SUBSTITUTION(a, b)
#undef min
and go with standard facilities.