I've started studying C++ recently, and I asked a friend who uses C++ at work on a daily basis about #ifndef and #define. He said that nobody uses because if someone writes proper code they are not neccesarry. However in the books (for beginners) I'm reading it is told to be a good practice to use them.
3 Answers
What if you want to use some OS specific features or want to write different code for different platforms? What if you want to be able to enable/disable certain features of your code?
Here comes the preprocessor and #ifdef
s, #define
s and #endif
s.
Suppose you want your code to work with some Windows- and Linux-specific features:
#ifdef WINDOWS
#include <something_windows_related.h>
#else
#include <posix.h>
#endif
This is often needed when working with OpenCL:
#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
If you want to switch on or off some feature when needed.
#ifdef HAVE_OPENCL
bool InitOpenCL(void) {
// some code
}
#endif
So, the answer is - these preprocessor directives are absolutely OK and sometimes are the only way to do certain things.
Well, as mentioned in my comment instead of using
#ifndef MYHEADER_
#define MYHEADER_
#endif
most modern compilers provide the
#pragma once
preprocessor directive to avoid multiple inclusion of header files.
But I'd still advise for the 1st form, as it's portable.
Also simply that #pragma once
exists, won't render the #ifndef
or #define
directives obsolete in whole, these are used for other things as well.
-
1What's that? you first answer the question and then close it? This seems rather poor practice. If you think the question can be sensibly answered, then why not allow others to do so?– WalterMar 28, 2016 at 13:20
-
1@Walter And that reasons for a downvote or what? Concentrate on content and not my behavior here. If you want to get the question reopened upvote it and vote to reopen. If you have doubts ask on meta please. Mar 28, 2016 at 13:23
To resolve any confusion about C++:
- c++ have a standard https://isocpp.org/std/the-standard. WARNING! C++ compliers are not garantee support of all std features. Others can support some own features.
- In my opinion best way to get vision about C++ coding/practice is watch inside Boost(http://www.boost.org) sources.
About your case.
Before write any line of code. You must find answer on the question: 'YYY compiler have support of XXX feature or not?'
For instance you wish write C++ std11 or std14 with using 'pragma once'. Target platforms/compilers are OSX, Windows and Apple CLang, vcc.
Via msdn search we get following https://msdn.microsoft.com/en-us/library/4141z1cx(v=vs.71).aspx https://msdn.microsoft.com/en-us/library/4141z1cx(v=vs.140).aspx vcc support pragma once start from vs 71 - later version.
So via google/stackoverflow/etc you can get information about any compilers and features. Using found information you can take decision use std/not std feature or not.
-
I am sorry but I do not understand this answer at all. How does this answer the question of whether or not
#ifndef
and#define
are obsolete? Mar 28, 2016 at 12:39 -
#ifndef and #define as guards is old practice. If you use new compiler with full support of new std you can use #pragma once instead of #ifndef/define. Also I say about where can see some C++ styles/practices. How-to search information about concrete compiler and features. Mar 28, 2016 at 12:57
-
#pragma once
is not standard. Not even in the newest standard. Most modern compilers support it but that does not make it standard. Mar 28, 2016 at 12:59 -
True. But as I as say: 'C++ compliers are not garantee support of all std features. Others can support some own features'. My answer is more about how-to get information about using any of std/not std feature. Mar 28, 2016 at 13:06
-
P.S. Probably I can edit my answ to make it more informative. My point: before write any line of code need to get information about target compilers. Mar 28, 2016 at 13:08
#pragma once
, but that doesn't really mean these preprocessor statements became obsolete.#ifndef #define ... #endif
#ifndef
and#define
being obsolete in whole is ridiculous.const
const objects). You would need to expand your question to explain what you're specifically asking about for it to become properly answerable.