Is #pragma once a safe include guard?

后端 未结 14 2358
青春惊慌失措
青春惊慌失措 2020-11-22 07:42

I\'ve read that there is some compiler optimization when using #pragma once which can result in faster compilation. I recognize that is non-standard, and thus

相关标签:
14条回答
  • 2020-11-22 08:07

    #pragma once does have one drawback (other than being non-standard) and that is if you have the same file in different locations (we have this because our build system copies files around) then the compiler will think these are different files.

    0 讨论(0)
  • 2020-11-22 08:10

    The main difference is that the compiler had to open the header file to read the include guard. In comparison, pragma once causes the compiler to keep track of the file and not do any file IO when it comes across another include for the same file. While that may sound negligible, it can easily scale up with huge projects, especially ones without good header include disciplines.

    That said, these days compilers (including GCC) are smart enough to treat include guards like pragma once. i.e. they dont open the file and avoid the file IO penalty.

    In compilers that dont support pragma I've seen manual implementations that are a little cumbersome..

    #ifdef FOO_H
    #include "foo.h"
    #endif
    

    I personally like #pragma once approach as it avoids the hassle of naming collisions and potential typo errors. It's also more elegant code by comparison. That said, for portable code, it shouldn't hurt to have both unless the compiler complains about it.

    0 讨论(0)
  • 2020-11-22 08:12

    Using gcc 3.4 and 4.1 on very large trees (sometimes making use of distcc), I have yet to see any speed up when using #pragma once in lieu of, or in combination with standard include guards.

    I really don't see how its worth potentially confusing older versions of gcc, or even other compilers since there's no real savings. I have not tried all of the various de-linters, but I'm willing to bet it will confuse many of them.

    I too wish it had been adopted early on, but I can see the argument "Why do we need that when ifndef works perfectly fine?". Given C's many dark corners and complexities, include guards are one of the easiest, self explaining things. If you have even a small knowledge of how the preprocessor works, they should be self explanatory.

    If you do observe a significant speed up, however, please update your question.

    0 讨论(0)
  • 2020-11-22 08:12

    Additional note to the people thinking that an automatic one-time-only inclusion of header files is always desired: I build code generators using double or multiple inclusion of header files since decades. Especially for generation of protocol library stubs I find it very comfortable to have a extremely portable and powerful code generator with no additional tools and languages. I'm not the only developer using this scheme as this blogs X-Macros show. This wouldn't be possible to do without the missing automatic guarding.

    0 讨论(0)
  • 2020-11-22 08:13

    I don't know about any performance benefits but it certainly works. I use it in all my C++ projects (granted I am using the MS compiler). I find it to be more effective than using

    #ifndef HEADERNAME_H
    #define HEADERNAME_H
    ...
    #endif
    

    It does the same job and doesn't populate the preprocessor with additional macros.

    GCC supports #pragma once officially as of version 3.4.

    0 讨论(0)
  • 2020-11-22 08:13

    GCC supports #pragma once since 3.4, see http://en.wikipedia.org/wiki/Pragma_once for further compiler support.

    The big upside I see on using #pragma once as opposed to include guards is to avoid copy/paste errors.

    Let's face it: most of us hardly start a new header file from scratch, but rather just copy an existing one and modify it to our needs. It is much easier to create a working template using #pragma once instead of include guards. The less I have to modify the template, the less I am likely to run into errors. Having the same include guard in different files leads to strange compiler errors and it takes some time to figure out what went wrong.

    TL;DR: #pragma once is easier to use.

    0 讨论(0)
提交回复
热议问题