One of my co-workers discovered an interesting item in the #ifdef/#ifndef entry of the help files for Microsoft Visual C++ 2005:
These directives are provided only for compatibility with previous versions of the language. The defined( identifier ) constant expression used with the #if directive is preferred.
(Update: The same statement is in the help file for Visual C++ 2003. I don’t know about Visual C++ 6.0, the version immediately prior to the 2003 release.)
Preferred by whom? And why?
The two are practically identical in use, and I tend to use both — “#if defined” is good for testing multiple things on a single line (combined with && or ||), “#ifdef” is more concise when you only need to test one thing (which is usually the case). So what’s the reason to “prefer” one over the other? A ‘net search doesn’t turn up any indication of one.
Microsoft doesn’t control either the C or C++ standards, so they can’t remove #ifdef from them. They also can’t unilaterally remove it from the compiler without irritating practically all of their customers (which they really can’t afford to do any further). Where do they get off claiming that one is “preferred”? It sounds to me like the kind of playground-bully attitude that Microsoft has become infamous for.
It’s obvious, they don’t want you to port programs, so they want to encourage them to be as proprietary as possible. This goes back to before .Net, when MS VC++ was really a proprietary dialect of C++, and to MS Java and J++, which tried to pollute Java with MFC stuff.
That would make sense if “#if defined” was a proprietary extension, but it isn’t — it, like #ifdef, has been in the specification for as long as I’ve been using C and C++, so far as I know. And that’s going on twenty-two years now.
I wasn’t aware of that. Strange, why bother complaining about #ifdef then? Can’t stand conciseness?
#include
include
main() {
if defined(FOO)
endif
}
Doesn’t work here.
no wonder, I forgot to #define FOO
nm. It’s getting late I guess.
🙂