The disgusting pragma of using GCC

Header defenders are necessary in almost all C ++ programs, but it’s a pain when naming rules are strictly followed, especially during refactoring. When using GCC (and many other compilers), we have an alternative in the preprocessor command #pragma once. The recommendations that I saw against using this command (for example, the lack of support before version 3.3) are not very convincing regarding my personal projects. I would use #pragma onceif possible.

All that said, this quote from the GCC website gives me a break:

Note that in general we do not recommend using pragmas; For more information, see Attributes of a Function .

Perhaps this only works my experience in C ++ at the lower level, but I do not see any explanation for this recommendation on the site that this link points to. Can someone explain the reasoning behind their recommendations in the (semi-annual) layman's conditions?

+4
source share
2 answers

The general recommendation comes from the fact that not only is there no guarantee that other compilers will implement #pragma once(or any other pragma), there is no guarantee that other compilers will implement the #pragma oncesame way that GCC does. Another compiler could legitimately give it a completely different meaning, or, even worse, subtly different. If you do not need portable code, you can ignore this recommendation.

#ifndef/#define , . , . , , .

+5

- ++, :

#if defined (_MSC_VER) \
    || (defined (__BORLANDC__) && __BORLANDC__ >= 0x0650) \
    || (defined (__COMO__) && __COMO_VERSION__ >= 400) /* ??? */ \
    || (defined (__DMC__) && __DMC__ >= 0x700) /* ??? */ \
    || (defined (__clang__) && __clang_major__ >= 3) \
    || (defined (__GNUC__) && (__GNUC__ >= 4 \
    || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)))
# define LOG4CPLUS_HAVE_PRAGMA_ONCE
# pragma once
#endif

, #pragma once . #pragma once.

, , , . , . OTOH, ++. .

+2

All Articles