Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What are these macros for?

What are these macros for?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Clint Westwood
    wrote on last edited by Clint Westwood
    #1

    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
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Clint Westwood
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SysTech
          wrote on last edited by
          #4

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

          #pragma once
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            Clint Westwood
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Clint Westwood
                wrote on last edited by
                #7

                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?

                JKSHJ Chris KawaC 2 Replies Last reply
                0
                • C Clint Westwood

                  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?

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @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).

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • C Clint Westwood

                    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?

                    Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved