Avatar ·

#pragma once vs include guards: what to choose to prevent multiple inclusion?

Hello everyone! I'm a beginner C++ developer and I've already encountered the problem of double inclusion of header files several times. For example, I have this code:

\n\n
// math_utils.h\n#ifndef MATH_UTILS_H\n#define MATH_UTILS_H\n\nint add(int a, int b) { return a + b; }\n\n#endif\n\n// main.cpp\n#include "math_utils.h"\n#include "math_utils.h"  // accidentally included twice\n\nint main() {\n    return add(2, 3);\n}\n
\n\n

With include guards everything works, but I noticed that many modern projects (for example, from Boost) use #pragma once. I read that this is a non-standard directive, but it is supported by all major compilers.

\n\n

I'm interested in:

\n
    \n
  • Are there any real performance issues with include guards?
  • \n