Search code examples
c++header-filespragma

Is there a good reason not to use #pragma once in every header file?


I'm new to C++ and started to organize my code like most people seem to do: in "header files" and "source files".

I'm using #pragma once in all those header files that contain inline functions. But I was wondering if there is a reason against simply using it in every header file?


Solution

  • I'm using #pragma once in all those header files that contain inline functions

    That's about the one reason that you don't need #pragma once. Having multiple definitions for the same inline function is normal and expected. And inevitable when multiple translation units #include the same header, particularly for template functions. The linker just picks one of the definitions it finds if the compiler didn't actually inline the function. You need #pragma once to avoid multiple definitions of the same type.