As many of you know, #pragma is a compiler specific macro.
#pragma comment allows the user to send messages to the compiler at compile-time. My problem is that I've heard that this is a bad practice from a couple of people. And I can't really get evidence that it's worth using but I keep seeing it over and over. Are they're any real disadvantages to it?
GCC 1.21, upon finding a #pragma directive, would instead attempt to launch commonly distributed Unix games such as NetHack and Rogue, or start Emacs running a simulation of the Towers of Hanoi.
The same rule applies as with everything that isn't portable. If you're absolutely sure your code will always be compiled with the same compiler, then there's no harm in using it. That's why VC++'s standard headers are littered with #pragma inclusion guards.
Yeah, it's not in the Standard. I'd like to use #pragma once in place of #ifndef XYZ #define XYZ ... #endif , but I avoid it. The include guards work everywhere properly.
Like most things in C/C++, it's there for a reason.
If you want to switch on or control certain compiler specific features, you often do this with pragmas and/or via compiler command line switches. Like everything else, it can be abused, but can be exactly what you need when used properly. Take record packing for example.
Recent GNU compilers complain about pragmas they don't understand. This is a good thing as different it's possible that a commonly named pragma may be implemented differently on different compilers. All pragmas should be wrapped in #ifdef statements.
To summarise, it's not wrong to use them. Use them minimally and wisely and always wrap them with #ifdef statements.
Thanks for the replies! Though I still have questions, mainly concerning why pragma isn't in the standard library but perhaps I can find other areas to search for this. I, of course, wouldn't mind it being answered here but for now I'll mark this as solved.