I have seen a class where someone named member variables min
and max
class A
{
public:
A();
~A();
bool min;
bool max;
...
};
with a constructor
A::A()
{
min=false;
max=true;
...
}
I have tried to rewrite it with usage of an initialization list:
A::A():min(false), max(true){}
but I have received an warning + error
warning C4003: not enough actual parameters for macro 'min'
error C2059: syntax error : ')'
because min
macro is defined in WinDef.h
Is it possible to use initialization list in this situation without renaming of the member variables?
If you have VS2013 or another modern compiler:
A():min {false}, max {true} { }
avoids the problem because min(a,b)
is a function-style macro and {
can't start its argument list.
This is specific to initializer lists, so I've reopened the question. The "duplicate" suggested addresses macro use in expression context, which is fundamentally different - you can use (min)
there.
Depending on your situation you have several options.
Use brace initialisation as suggested by @MSalters
Define NOMINMAX
before you include the windows header files to avoid the min
and max
macros from being defined. This will depend on how big your code is and how much control you have over it.
Just #undef
the macros. Only recommend in a source file.
Use MSVC pre-processor directives to save, undef then restore the macros. Like so...
#pragma push_macro("min") #undef min /* use min here */ #pragma pop_macro("min")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.