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. void-ing the expression (as in q_unused)

void-ing the expression (as in q_unused)

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.5k Views 2 Watching
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    qt defines Q_UNUSED as follows:

    #define Q_UNUSED(x) (void)x;
    

    isn't void-ing the expression an old way to do things? is there a modern way (and why qt isn't using it)?

    jsulmJ 1 Reply Last reply
    0
    • U user4592357

      qt defines Q_UNUSED as follows:

      #define Q_UNUSED(x) (void)x;
      

      isn't void-ing the expression an old way to do things? is there a modern way (and why qt isn't using it)?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @user4592357 The only alternative I'm aware of is to omit parameter name:

      void someFunction(int, int)
      

      Or you can comment out the parameter names:

      void someFunction(int /*param1*/, int /*param2*/)
      

      But using Q_UNUSED makes it clear that the parameter is not used by intent.

      In C++17 there is this: https://en.cppreference.com/w/cpp/language/attributes/maybe_unused
      But since Qt needs to support older compilers as well (currently min requirement is C++11) this can't be used by Qt itself.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        while we are here, I never understood why Q_UNUSED is preferred to omitting the parameters. the latter actually forces the compiler to make sure it's unused but there's nothing, not even a warning, if you use an argument that was previously marked as unused

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        U kshegunovK 2 Replies Last reply
        0
        • VRoninV VRonin

          while we are here, I never understood why Q_UNUSED is preferred to omitting the parameters. the latter actually forces the compiler to make sure it's unused but there's nothing, not even a warning, if you use an argument that was previously marked as unused

          U Offline
          U Offline
          user4592357
          wrote on last edited by
          #4

          @VRonin
          maybe because it's a Q(something)?

          1 Reply Last reply
          0
          • VRoninV VRonin

            while we are here, I never understood why Q_UNUSED is preferred to omitting the parameters. the latter actually forces the compiler to make sure it's unused but there's nothing, not even a warning, if you use an argument that was previously marked as unused

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            qdoc uses the source to generate the documentation, thus it uses the parameter name from the function. If you don't specify a parameter name you'd break the docs. I personally prefer to just omit the parameter in my code (wherever possible), but that's not that straightforwardly attainable in Qt's codebase.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            3

            • Login

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