Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Having to do the #ifndef/#define/#endif dance in every header file I write

I'm just using "#pragma once" in my current hobby C++ project. I understand pragmas are living in sin, but is there any reason I shouldn't use them?



#pragma once aside, I agree that not having to worry about writing and including headers at all is a significant improvement of Go over C++.


It's not part of the C++ standard, so you're relying on a compiler extension. In theory, your code could blowup if you tried a different compiler, but in practice, most compilers you would use implement it. In general, pragmas are for compiler extensions - that is, things not defined in the language spec.


I wouldn't use "#pragma once", but it's also a straightforward thing to change later. If that turns out to be the only thing you need to refactor away from later as the project evolves, you're doing great.


gcc 3.x had #pragma once bugs related to symbolic links. MSVC had bugs related to filenames that only differed by case. Modern gcc and MSVC can recognize and optimize for the #include guard pattern. Many benchmarks show no build time improvement of traditional #include guards vs #pragma once.

I some one benchmark where the developer #included all his headers and source files into a single .cpp file. Compiling this single .cpp file was about 50x faster than a traditional build! <:) Of course, you will likely have name and dependency conflicts.


Not really anymore, it's supported on most compilers these days. But that's only been the case as of pretty recent.

The same could probably not be said if you were writing C instead of C++.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: