Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

C++: pragma once

📅 2011-May-30 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, include guards, pragma once, visual cpp ⬩ 📚 Archive

With both Visual C++ and GCC supporting pragma once completely, I do not see any reason to still use the include guards. Include guards were always a messy solution and I am glad that their death certificate has been written. For example, I would typically use a ALL_CAPS version of the header filename as the defined name. But then, one had to remember to change this whenever the file was renamed. Also, an include guard involved two pieces of code (the #ifndef-#define and the #endif) between which the header code was placed, again too messy and problematic.

In comparison, using #pragma once is straightforward. As a bonus, the horrendous C++ compilation time might be reduced a bit due to optimizations for pragma once by both these compilers.

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧