How to get around thoes system defines

Started by
12 comments, last by magic_man 14 years, 5 months ago
This is an issue with quite frankly pisses me off and it is done by development teams that do not think about things. Take std::max and windows max macro for an example, yes you can use another define to correct it or enclose std::max in braces but it is just evil, who every thought max was a good name for a macro needs taken outside and face at best some rotten fruit for a decade. What ever happened to the normal practice of using all caps?
Just like header guards should be unique so must all macros which are not part of the language standard. It does not take much to try and do this just pre or postfix the macro with the project acronym, MS_CreateFile or better still MS_CREATEFILE for example. This is something which teams still do not do today and hit me again the other day whilst trying to use two different testing frameworks to do different things with until both of the used ASSERT_EQUAL. Now granted Microsoft are not doing to do this for macros which have been used for years and would break code but it should be done for any newly defined macros.
/rant over.
"You insulted me!" I did not say that in the private message Tom Sloper!
Advertisement
Quote:Original post by magic_man
Now granted Microsoft are not doing to do this for macros which have been used for years and would break code but it should be done for any newly defined macros.
/rant over.
Most of Microsoft's new code is Unicode-only (for example, there's no TaskDialogA and TaskDialogW, just TaskDialog), so at least there's no new macros.

And unfortunately, namespaces are out of the question, since Win32 is a C API. Of course, they could've gone with something like GTK's naming convention which is a pretty good compromise if you ask me, but it's a bit late for regrets now :)
You could compile your platform-specific(read:windows) code separately and link it, this would also abstract it so that you can make your code multi-platform. You'd just have a link error on each system until you make you new platform lib for each platform.
Quote:Original post by magic_man
Take std::max and windows max macro for an example
This particular thing can be turned off by defining NOMINMAX (which I've defined as default compiler option, so it works on all files in all projects).

But you're of course absolutely right, it's super intrusive and annoying, especially since it's not only about min/max and CreateFile, but dozens of other common names that you might incidentially use (GetMessage, CreateFont, DrawText, anyone?).

The good news is that if you consistenly include windows.h everywhere, then even though it screws up your names, it screws them up everywhere in the same way, so your code will usually still work.
Quote:Original post by samoth
Quote:Original post by magic_man
Take std::max and windows max macro for an example
This particular thing can be turned off by defining NOMINMAX (which I've defined as default compiler option, so it works on all files in all projects).

Yes I did mention that :)
Although sometimes that is the wrong option to choose.
Say for example you provide template headers which should be included in a users code, forcing the user to use the macro seems just as wrong to me. This is why for this I prefer to wrap it in parenthesis.
For example
T instance_of_max ((std::numeric_limits<T >::max)());


"You insulted me!" I did not say that in the private message Tom Sloper!

This topic is closed to new replies.

Advertisement