Reputation: 3
I have code:
#if _MSC_VER <= 1300
float round(float f)
{
if (f < 0)
return ceilf (f - 0.5);
else
return floorf (f + 0.5);
}
#endif
Lines above should compile only in old version of Visual C++ compiler.
I compill this code with MinGW compiller. There is no such symbol like _MSC_VER there, and it code doesn't have to compile, because the expression #if _MSC_VER <= 1300
must equals false. But, it compiles.
Could somebody explain me why does it happen?
Compile in MinGW is GNU 6.3.0.
Upvotes: 0
Views: 842
Reputation: 8497
Well, on g++ _MSC_VER
is not defined, as You noted it's Visual C++ specific.
You could try adding the following:
#ifdef _MSC_VER
#if _MSC_VER <= 1300
// Your code
#endif
#endif
Also, if I'm reading the C++ standard right, the undefined identifier is replaced with 0, so it passes Your conditional and compiles as if You were using that "ancient Visual C++".
Excerpt from 16.1 Conditional inclusion:
After all replacements due to macro expansion and the defined unary operator have been performed, all remaining identifiers and keywords, except for true and false, are replaced with the pp-number 0, and then each preprocessing token is converted into a token.
Upvotes: 2
Reputation: 234635
If _MSC_VER
is not defined then the compiler will not see any of the code from and including #if
to and including #endif
.
Depending on the context, the compiler will see valid source code, and compile it with success. Rest assured that your version of round
will not form part of the compiled program, although std::round
might have been implicitly included somewhere.
Finally, there are faults with using an additive constant of 0.5
to engineer a round
function. See Why do lots of (old) programs use floor(0.5 + input) instead of round(input)?
Upvotes: 0