Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. To static or not to static?
Forum Updated to NodeBB v4.3 + New Features

To static or not to static?

Scheduled Pinned Locked Moved Solved C++ Gurus
19 Posts 5 Posters 1.8k 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.
  • T TomZ
    28 Feb 2024, 11:22

    @JonB static methods on a class should, in my opinion, be very strongly related to the class.
    Which means things like factory methods and things like QObject::connect

    So, no, I'd not make it a static method since it doesn't make sense to do so.

    In C++ the thing to worry about is exportable symbols. The ideal is that you have a method that does not leave the cpp file and there static, or simply placing it in an anonymous namespace make sense. It avoids its symbols being seen by the linker.

    If you need it in multiple cpp files, I tend to make a private class or namespace where I place them on. Namespace is nice for "static" like methods here. Avoid installing the header file (I follow Qts example of having _p.h as extension) to make it as private as you can.

    I personally try to avoid "polluting" a header file with methods that are pure implementation specific. The example qualifies as its pretty cheap to just hide it from the outside world in the ways I suggested.

    J Online
    J Online
    JonB
    wrote on 28 Feb 2024, 11:37 last edited by
    #9

    @TomZ
    Two votes out of two responses for not doing static on a (private) method which does not access this. Shame for not being as "tidy" as I like it in C#, but if that's the C++ convention I will change over to it (obviously marking them as const instead)....

    Will await any further opinions and mark this as "Solved" later.

    J 1 Reply Last reply 28 Feb 2024, 11:39
    0
    • J JonB
      28 Feb 2024, 11:37

      @TomZ
      Two votes out of two responses for not doing static on a (private) method which does not access this. Shame for not being as "tidy" as I like it in C#, but if that's the C++ convention I will change over to it (obviously marking them as const instead)....

      Will await any further opinions and mark this as "Solved" later.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 28 Feb 2024, 11:39 last edited by
      #10

      @JonB const, noexcept and [[nodiscard]] where ever possible ! :D


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      J 1 Reply Last reply 28 Feb 2024, 11:41
      1
      • J J.Hilk
        28 Feb 2024, 11:39

        @JonB const, noexcept and [[nodiscard]] where ever possible ! :D

        J Online
        J Online
        JonB
        wrote on 28 Feb 2024, 11:41 last edited by JonB
        #11

        @J-Hilk If you think I'm adding that sort of stuff to all my functions I'm not going to! :) I don't think even Qt headers do the "noexcept and [[nodiscard]] " mostly.

        J 1 Reply Last reply 28 Feb 2024, 11:50
        0
        • J JonB
          28 Feb 2024, 11:41

          @J-Hilk If you think I'm adding that sort of stuff to all my functions I'm not going to! :) I don't think even Qt headers do the "noexcept and [[nodiscard]] " mostly.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 28 Feb 2024, 11:50 last edited by
          #12

          @JonB this actually would be good practice :P


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          J 1 Reply Last reply 28 Feb 2024, 11:52
          0
          • J J.Hilk
            28 Feb 2024, 11:50

            @JonB this actually would be good practice :P

            J Online
            J Online
            JonB
            wrote on 28 Feb 2024, 11:52 last edited by
            #13

            @J-Hilk It would, but it's too much typing every time to change the defaults. If I wanted that I'd consider changing to Rust ;-)

            J 1 Reply Last reply 28 Feb 2024, 11:56
            1
            • J JonB
              28 Feb 2024, 11:52

              @J-Hilk It would, but it's too much typing every time to change the defaults. If I wanted that I'd consider changing to Rust ;-)

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 28 Feb 2024, 11:56 last edited by
              #14

              @JonB to bad that people refuse to change the standard or at least to add a flag to the compiler to change the default interpretations.

              Immagine people having to write [[maybe_unused]] or mutable, the horror!


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mranger90
                wrote on 28 Feb 2024, 12:46 last edited by
                #15

                For what its worth, the coding guidelines for my company say that such methods should be external to the class. We tend to bundle them up in common libraries and wrap them in their own namespace.

                J 1 Reply Last reply 28 Feb 2024, 12:51
                0
                • M mranger90
                  28 Feb 2024, 12:46

                  For what its worth, the coding guidelines for my company say that such methods should be external to the class. We tend to bundle them up in common libraries and wrap them in their own namespace.

                  J Online
                  J Online
                  JonB
                  wrote on 28 Feb 2024, 12:51 last edited by JonB
                  #16

                  @mranger90
                  FWIW, personally I find that an extraordinarily burdensome guideline. Small utility methods which happen not to use this but are useful privately in a class (and perhaps meaningless elsewhere) are not at all uncommon. (I haven't done so, but I would be surprised if I don't find such in, say, Qt sources.) Having to move them out does not seem nice to me. But what do I know, I respect you say that is what your company requires.

                  EDIT
                  I don't have downloaded Qt sources. Just using woboq for Qt5 and having a look at, say, QString for private static methods I happenstanced on, say, declaration https://codebrowser.dev/qt5/qtbase/src/corelib/text/qstring.h.html#_ZN7QString14compare_helperEPK5QChariS2_iN2Qt15CaseSensitivityE

                  private:
                      static int compare_helper(const QChar *data1, int length1,
                                                const QChar *data2, int length2,
                                                Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
                  

                  definition https://codebrowser.dev/qt5/qtbase/src/corelib/text/qstring.cpp.html#_ZN7QString14compare_helperEPK5QChariS2_iN2Qt15CaseSensitivityE

                  /*!
                      \internal
                      \since 4.5
                  */
                  int QString::compare_helper(const QChar *data1, int length1, const QChar *data2, int length2,
                                              Qt::CaseSensitivity cs) noexcept
                  {
                      Q_ASSERT(length1 >= 0);
                      Q_ASSERT(length2 >= 0);
                      Q_ASSERT(data1 || length1 == 0);
                      Q_ASSERT(data2 || length2 == 0);
                      return qt_compare_strings(QStringView(data1, length1), QStringView(data2, length2), cs);
                  }
                  

                  A perfect example of a private utility method that they seem to declare static as I would have done (though I have not checked whether it might be called from some static method).

                  J 1 Reply Last reply 28 Feb 2024, 15:17
                  0
                  • J JonB
                    28 Feb 2024, 12:51

                    @mranger90
                    FWIW, personally I find that an extraordinarily burdensome guideline. Small utility methods which happen not to use this but are useful privately in a class (and perhaps meaningless elsewhere) are not at all uncommon. (I haven't done so, but I would be surprised if I don't find such in, say, Qt sources.) Having to move them out does not seem nice to me. But what do I know, I respect you say that is what your company requires.

                    EDIT
                    I don't have downloaded Qt sources. Just using woboq for Qt5 and having a look at, say, QString for private static methods I happenstanced on, say, declaration https://codebrowser.dev/qt5/qtbase/src/corelib/text/qstring.h.html#_ZN7QString14compare_helperEPK5QChariS2_iN2Qt15CaseSensitivityE

                    private:
                        static int compare_helper(const QChar *data1, int length1,
                                                  const QChar *data2, int length2,
                                                  Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
                    

                    definition https://codebrowser.dev/qt5/qtbase/src/corelib/text/qstring.cpp.html#_ZN7QString14compare_helperEPK5QChariS2_iN2Qt15CaseSensitivityE

                    /*!
                        \internal
                        \since 4.5
                    */
                    int QString::compare_helper(const QChar *data1, int length1, const QChar *data2, int length2,
                                                Qt::CaseSensitivity cs) noexcept
                    {
                        Q_ASSERT(length1 >= 0);
                        Q_ASSERT(length2 >= 0);
                        Q_ASSERT(data1 || length1 == 0);
                        Q_ASSERT(data2 || length2 == 0);
                        return qt_compare_strings(QStringView(data1, length1), QStringView(data2, length2), cs);
                    }
                    

                    A perfect example of a private utility method that they seem to declare static as I would have done (though I have not checked whether it might be called from some static method).

                    J Offline
                    J Offline
                    JoeCFD
                    wrote on 28 Feb 2024, 15:17 last edited by JoeCFD
                    #17

                    @JonB said in To static or not to static?:

                    compare_helper

                    qt_compare_strings is static as well. That may be the reason why compare_helper is static. It does have to be, I guess. If not, the return is different.
                    return QString::qt_compare_strings(QStringView(data1, length1), QStringView(data2, length2), cs);

                    If the static function performs operations that are related to the class as a whole, rather than specific instances, it may make sense to keep it static and private.

                    I do not know if it is a good practice or not. all math funcs for exampes std::cos are defined in namingspace std. The reason could be that template for class member funcs were introduced in C++ 14. In or before C++ 11, it was not possible to use template for member funcs in a class.

                    J 1 Reply Last reply 28 Feb 2024, 15:21
                    0
                    • J JoeCFD
                      28 Feb 2024, 15:17

                      @JonB said in To static or not to static?:

                      compare_helper

                      qt_compare_strings is static as well. That may be the reason why compare_helper is static. It does have to be, I guess. If not, the return is different.
                      return QString::qt_compare_strings(QStringView(data1, length1), QStringView(data2, length2), cs);

                      If the static function performs operations that are related to the class as a whole, rather than specific instances, it may make sense to keep it static and private.

                      I do not know if it is a good practice or not. all math funcs for exampes std::cos are defined in namingspace std. The reason could be that template for class member funcs were introduced in C++ 14. In or before C++ 11, it was not possible to use template for member funcs in a class.

                      J Online
                      J Online
                      JonB
                      wrote on 28 Feb 2024, 15:21 last edited by JonB
                      #18

                      @JoeCFD said in To static or not to static?:

                      qt_compare_strings is static as well. That may be the reason why compare_helper is static

                      Sorry, that does not make sense. You can of course call a static function from a non-static member function, just not the other way round. This would not be any reason to choose to make the calling method, QString::compare_helper(), be static.

                      it may make sense to keep it static and private

                      But you can see the other responders here are suggesting I do not make private methods static, which I am now following.

                      J 1 Reply Last reply 28 Feb 2024, 15:24
                      0
                      • J JonB
                        28 Feb 2024, 15:21

                        @JoeCFD said in To static or not to static?:

                        qt_compare_strings is static as well. That may be the reason why compare_helper is static

                        Sorry, that does not make sense. You can of course call a static function from a non-static member function, just not the other way round. This would not be any reason to choose to make the calling method, QString::compare_helper(), be static.

                        it may make sense to keep it static and private

                        But you can see the other responders here are suggesting I do not make private methods static, which I am now following.

                        J Offline
                        J Offline
                        JoeCFD
                        wrote on 28 Feb 2024, 15:24 last edited by
                        #19

                        @JonB I guess both ways work. I do not have preference.

                        1 Reply Last reply
                        0
                        • J JonB has marked this topic as solved on 28 Feb 2024, 15:26

                        18/19

                        28 Feb 2024, 15:21

                        • Login

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