What are these macros for?



  • This is automatically printed whenever I create a new header file in Qt Creator. It doesn't seem like it's really needed, so I want to remove these macros. But before I do it I'd like to make sure I'm not missing any Qt peculiarities. What are they for?

    #ifndef MY_CLASS
    #define MY_CLASS
    
    // all the code here
    
    #endif // MY_CLASS
    

  • Lifetime Qt Champion

    Hi,

    That's nothing Qt specific, it's called include guard. Don't remove them.



  • I understand now, thanks. Seems to be extermely useful once project grows into large proportions.



  • You can remove them and put this single item at the top (with most compilers)

    #pragma once


  • @SysTech said:

    You can remove them and put this single item at the top (with most compilers)

    #pragma once
    

    Thanks, I'll make sure to include this from now on.


  • Lifetime Qt Champion

    There's one thing to consider: it's a non standard extension so even if it's widely supported, there are some limitation that you should take into account first before replacing all your guards with pragma once



  • From what I've read so far it seems that it's better to use pragma once for different include files, while include guards are better suited for copy-pasting same file around project (though why would someone need to do that is unknown to me). Am I correct?


  • Moderators

    @Clint-Westwood said:

    From what I've read so far it seems that it's better to use pragma once for different include files, while include guards are better suited for copy-pasting same file around project (though why would someone need to do that is unknown to me). Am I correct?

    The pragma and the include guards are designed for exactly the same purpose. #pragma once was invented simply because someone felt the same way as you did about include guards. It is nicer to write one line instead of 3, after all.

    As for which is "better" depends on your requirements. If you only intend to support one (or a few) of the major compilers which do support #pragma once, then go for it. If you intend to support exotic platforms that require custom compilers that might not support the pragma, then include guards are "better" (because the pragma might break your code).


  • Moderators

    @Clint-Westwood said:

    From what I've read so far it seems that it's better to use pragma once for different include files, while include guards are better suited for copy-pasting same file around project

    It's the other way around. Include guard macros need to have unique names to work properly, so it's less error prone to use pragmas when you copy/paste files around. With macros you can forget to rename it.
    Apart form the exotic target compiler support JKSH mentioned it's mostly a stylistic choice (do I want to use non-standard extensions). Practically there's little to worry about as all the big ones (MSVC, GCC, Clang, ICC) support it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.