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

While I'm on the subject the same also goes for header include guards - when you get a conflict, it's actually quite annoying to track down. (Not least because it's such a rare occurrence that you probably won't expect it and will likely end up on a wild goose chase at some inconvenient moment.)

I stopped using the file name at all in my include guards a few years ago, and use a GUID instead. For example:

    #ifndef HEADER_6AFF21D71B5B43DEB079AA612E4118B4
    #define HEADER_6AFF21D71B5B43DEB079AA612E4118B4

    #endif//HEADER_6AFF21D71B5B43DEB079AA612E4118B4
Also consider the use of #pragma once - though as far as I can tell, this (still) isn't ISO, so I've decided to avoid it.


> Also consider the use of #pragma once - though as far as I can tell, this (still) isn't ISO, so I've decided to avoid it.

Depends on your target platform. GCC, clang/LLVM, Visual C++, and many proprietary compilers all support it. What platform are you targeting that doesn't support it?

Because if you're writing any non-trivial useful code, it's highly likely that your code is not pure ISO C; you're using some library with support for various platforms, or a system call interface, or some other interface to a real system. Once you do that, universal portability no longer applies, so you might as well think about which specific target platforms you care about.


Good question - probably the main reason (and you don't have to agree that it's a good one) is that I'd never have to think about it again ;)


Unless you're working on embedded hardware with proprietary compilers, #pragma once is supported by all compilers (see https://en.wikipedia.org/wiki/Pragma_once). You can safely assume it'll be available on your platform.




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

Search: