7

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.

user137425
  • 423
  • 5
  • 16
  • 1
    Guessing you are asking about include guards, most modern compilers support `#pragma once`, but that doesn't really mean these preprocessor statements became obsolete. – πάντα ῥεῖ Mar 28 '16 at 11:55
  • 6
    Don't listen to your friend. The only standard, portable way to create include guards is to use `#ifndef #define ... #endif` – NathanOliver Mar 28 '16 at 11:56
  • 1
    This questions is **not about include guards**, and hence shouldn't have been closed (for the reason given). – Walter Mar 28 '16 at 13:17
  • 4
    @Walter The question is about _include guards_ unless the OP states something different (see the [tag:include] tag, a subtle hint). Claiming `#ifndef` and `#define` being obsolete in whole is ridiculous. – πάντα ῥεῖ Mar 28 '16 at 13:34
  • 1
    Your "friend" is insane. Or perhaps should take a few of those days off work to go on a training course. _Or_ you misunderstood him, because things like functional macros and using macros for defining constants _are_, by and large, considered outdated and obsolete (in preference for nice inline functions and honest-to-god `const` const objects). You would need to expand your question to explain what you're _specifically_ asking about for it to become properly answerable. – Lightness Races in Orbit Mar 28 '16 at 13:53
  • When was god honest (I the unlikely event) he exists? But he doesn't and therfore this can be a long chat – Ed Heal Mar 28 '16 at 14:08
  • @πάνταῥεῖ A, the tag -- I missed that. – Walter Mar 29 '16 at 18:52

3 Answers3

5

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 #ifdefs, #defines and #endifs.

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.

ForceBru
  • 36,993
  • 10
  • 54
  • 78
0

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.

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
  • 1
    What'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? – Walter Mar 28 '16 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 '16 at 13:23
0

To resolve any confusion about C++:

  1. 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.
  2. 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? – NathanOliver Mar 28 '16 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. – Vasiliy Soshnikov Mar 28 '16 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. – NathanOliver Mar 28 '16 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. – Vasiliy Soshnikov Mar 28 '16 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. – Vasiliy Soshnikov Mar 28 '16 at 13:08