← Back to context

Comment by kookamamie

4 days ago

No, it is the way. Edit: no one has time for inventing unique names for include guards.

Does anyone write those by hand anyway in any kind of project the size where it would matter?

#pragma once is broken by design

  • > Does anyone write those by hand anyway in any kind of project the size where it would matter?

    I think you're suggesting that you don't need to make up the names for include guards because all tools / IDEs for C++ write them for you automatically anyway. But that isn't my experience. Many IDEs don't write include guards for you automatically ... because everybody uses #pragma once already.

    > #pragma once is broken by design

    I think you're referring to the historical problem with #pragma once, which is that it can be hard for the compiler to identify what is really the same file (and therefore shouldn't be included a second time). If you hard link to the same file, or soft link to it, is it the same? What if the same file is mapped to two different mount points? What if genuinely different files have the same contents (e.g., because the same library is included from two different installation paths)? In practice, soft/hard links to the same file are easily detectable, and anything more obscure indicates such a weird problem with your setup that you surely have bigger issues. #pragma once is fine.

    (Historically, it also had the benefit that compilers would know not to even re-read the header, whereas with traditional include guards they would need to re-include the file (e.g. in case the whole file is not wrapped in the #ifdef, or in case something else has undefined it since) only to then discard the contents. I've even seen coding guidelines requiring external include guards wrapped around every use of headers with #include <...>. Yuck! But modern compilers can work out when include guards are meant to mean that so today that difference probably no longer exists.)

  • I don't understand what you're saying here. #pragma once does the job that include guards used to do, but with less work, and in a less error prone way. How is it broken, and how is the size of a project relevant?

    • > I don't understand what you're saying here. #pragma once does the job that include guards used to do, (...)

      They don't. They are not C++ and at most they are compiler-specific.

      It's fine if you opt to not write C++ and instead target specific compilers instead. Just don't pretend it's not frowned upon or kosher.

      2 replies →

> No, it is the way.

No, this is completely wrong. Pragma once is non-standard compiler directive. It might be supported by some compilers such as msvc but technically it is not even C++.

There are only two options: include guards, and modules.

  • What major compiler does not support it?

    • > What major compiler does not support it?

      The whole point is that it's not supported and it's not standard, thus using #pragma once needlessly introduced the risk of having the code break.

      You should ask yourself what are you doing and why are you using non-standard constructs that may or may not work, specially when it's rather obvious and trivial to just use include guards. Using #pragma once isn't even qualify as being clever to gain anything.

      5 replies →