Is #pragma once a safe include guard?

后端 未结 14 2357
青春惊慌失措
青春惊慌失措 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:14

    I use it and I'm happy with it, as I have to type much less to make a new header. It worked fine for me in three platforms: Windows, Mac and Linux.

    I don't have any performance information but I believe that the difference between #pragma and the include guard will be nothing comparing to the slowness of parsing the C++ grammar. That's the real problem. Try to compile the same number of files and lines with a C# compiler for example, to see the difference.

    In the end, using the guard or the pragma, won't matter at all.

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

    I wish #pragma once (or something like it) had been in the standard. Include guards aren't a real big deal (but they do seem to be a little difficult to explain to people learning the language), but it seems like a minor annoyance that could have been avoided.

    In fact, since 99.98% of the time, the #pragma once behavior is the desired behavior, it would have been nice if preventing multiple inclusion of a header was automatically handled by the compiler, with a #pragma or something to allow double including.

    But we have what we have (except that you might not have #pragma once).

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