Is using #ifndef and #define in C ++ deprecated?

I recently started learning C ++, and I asked a friend who uses C ++ daily to work on #ifndef and #define. He said that no one is using, because if someone writes the correct code, they are not necessary. However, in books (for beginners) I read that it is said that it is good practice to use them.

+4
source share
3 answers

What if you want to use some specific OS or want to write different code for different platforms? What if you want to enable / disable some functions of your code?

Here is the preprocessor of both #ifdefs #defineand #endifs.

, , Windows Linux:

#ifdef WINDOWS
#include <something_windows_related.h>
#else
#include <posix.h>
#endif

OpenCL:

#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif

, .

#ifdef HAVE_OPENCL
bool InitOpenCL(void) {
    // some code
}
#endif

, : .

+4

++:

.

. : " YYY XXX ?"

, ++ std11 std14 "pragma once". / OSX, Windows Apple CLang, vcc.

msdn 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 pragma .

, google/stackoverflow/etc . , std/not std .

+1

,

#ifndef MYHEADER_
#define MYHEADER_
#endif

most modern compilers provide

#pragma once

preprocessor to avoid multiple inclusion of header files.

But I would still advise the 1st form, since it is portable.

It is also simple that what #pragma onceexists will not output directives #ifndefor be #definedeprecated in general; they are also used for other things.

0
source

All Articles