Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. clang complaint about string comparison
Servers for Qt installer are currently down

clang complaint about string comparison

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
10 Posts 4 Posters 1.9k Views 4 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.
  • K Offline
    K Offline
    koahnig
    wrote on 5 Aug 2018, 14:07 last edited by
    #1

    I am still getting used to clang model in new creator version

    Today I stumbled over where str is std::string.

    //    if ( str.substr( 0, 9 ) == "Name week")
        if ( ! strncmp ( str.c_str(), "Name week", 9 ) )
    

    the commented line got the complaint this is not defined and one should use "strncmp". I am not really a C++ purist and can accept things when working.
    However, that construct was not really going down well and I used google to find a bit of explanation. std::string has even its own compare function since c++98 standard.

    Personally I try to get away from pure C and tend to go for C++.

    Therefore a couple of questions thrown to the experts:
    Why the recommendation is to strncmp and not string::compare?
    Is this because std::string is standard template and not Qt?
    Also does one know why the direct comparison is not a good choice? At first sight possible ambiguities are not obvious.

    Also the clang modeler is a mighty tool, I am certainly impressed. However, when changing to strncmp it did not tell that the appropriate include is missing. ;)

    Vote the answer(s) that helped you to solve your issue(s)

    J 1 Reply Last reply 7 Aug 2018, 06:08
    0
    • A Offline
      A Offline
      ambershark
      wrote on 7 Aug 2018, 00:23 last edited by
      #2

      I can't answer all the questions you have but I can say you should definitely be using string::compare if you using std::string. I look at the strncmp as incorrect. It's odd that the clang modeler is viewing it as wrong. I would say that this is just a bug in the modeler more than a reason to switch.

      Personally I try to get away from pure C and tend to go for C++.

      If you are being a C++ purist you don't want to use C string compare functions for sure. Now-a-days People that learn C++ from scratch probably don't even know the old C functions since STL has all that for them now. C and C++ aren't as mixed as they used to be with the C++98/11/17/etc standards.

      Also does one know why the direct comparison is not a good choice?

      I think it is the good/proper choice. :)

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      4
      • A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 7 Aug 2018, 04:53 last edited by aha_1980 8 Jul 2018, 06:13
        #3

        hi all,

        just a guess: the second parameter is a string constant, and it may be cheaper to convert with c_str() (effective a pointer) than converting the constant to std::string.

        that all is given there is no const char * overload for compare (which I haven't checked).

        Edit: or it is rather the substr() which is not cheap. does it allocate?

        Edit2: try compare() as @ambershark suggested. it shoul work best.

        Qt has to stay free or it will die.

        1 Reply Last reply
        3
        • K koahnig
          5 Aug 2018, 14:07

          I am still getting used to clang model in new creator version

          Today I stumbled over where str is std::string.

          //    if ( str.substr( 0, 9 ) == "Name week")
              if ( ! strncmp ( str.c_str(), "Name week", 9 ) )
          

          the commented line got the complaint this is not defined and one should use "strncmp". I am not really a C++ purist and can accept things when working.
          However, that construct was not really going down well and I used google to find a bit of explanation. std::string has even its own compare function since c++98 standard.

          Personally I try to get away from pure C and tend to go for C++.

          Therefore a couple of questions thrown to the experts:
          Why the recommendation is to strncmp and not string::compare?
          Is this because std::string is standard template and not Qt?
          Also does one know why the direct comparison is not a good choice? At first sight possible ambiguities are not obvious.

          Also the clang modeler is a mighty tool, I am certainly impressed. However, when changing to strncmp it did not tell that the appropriate include is missing. ;)

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 7 Aug 2018, 06:08 last edited by
          #4

          @koahnig what version of QtCreator are we talking about, the 4.7rc ?
          Because with 4.6.2 , this:

          std::string str("Name week blablabla");
              if ( str.substr( 0, 9 ) == "Name week")
                  qDebug() << "is substr";
          

          does not create a warning for me.


          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.

          A K 2 Replies Last reply 7 Aug 2018, 06:42
          0
          • J J.Hilk
            7 Aug 2018, 06:08

            @koahnig what version of QtCreator are we talking about, the 4.7rc ?
            Because with 4.6.2 , this:

            std::string str("Name week blablabla");
                if ( str.substr( 0, 9 ) == "Name week")
                    qDebug() << "is substr";
            

            does not create a warning for me.

            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 7 Aug 2018, 06:42 last edited by
            #5

            @J.Hilk Do you have Clang Code Model enabled?

            Qt has to stay free or it will die.

            J 1 Reply Last reply 7 Aug 2018, 06:45
            0
            • J J.Hilk
              7 Aug 2018, 06:08

              @koahnig what version of QtCreator are we talking about, the 4.7rc ?
              Because with 4.6.2 , this:

              std::string str("Name week blablabla");
                  if ( str.substr( 0, 9 ) == "Name week")
                      qDebug() << "is substr";
              

              does not create a warning for me.

              K Offline
              K Offline
              koahnig
              wrote on 7 Aug 2018, 06:45 last edited by
              #6

              @J.Hilk said in clang complaint about string comparison:

              @koahnig what version of QtCreator are we talking about, the 4.7rc ?
              Because with 4.6.2 , this:

              std::string str("Name week blablabla");
                  if ( str.substr( 0, 9 ) == "Name week")
                      qDebug() << "is substr";
              

              does not create a warning for me.

              I have already upgraded to 4.7.0 as soon as it came out. I doubt that you see it with 4.6.2

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • A aha_1980
                7 Aug 2018, 06:42

                @J.Hilk Do you have Clang Code Model enabled?

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 7 Aug 2018, 06:45 last edited by
                #7

                @aha_1980
                oh, you're right, Iassumed it was a new default feature, but it's on all my PCs disabled because the corresponding plugin is not loaded

                Well, I'm out of the discussion than :(


                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
                • J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 7 Aug 2018, 06:57 last edited by
                  #8

                  I didn't realize, that 4.7 was released, its still in my Preview in the maintenance repo, but I was able to update on my macos device.

                  So, under what diagnostic configuration das this accure? For me, it's by default set to Clang-only checks for almost everything [build-in] and I don't have a warning with the above mentioned code.


                  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.

                  K 1 Reply Last reply 7 Aug 2018, 09:01
                  1
                  • J J.Hilk
                    7 Aug 2018, 06:57

                    I didn't realize, that 4.7 was released, its still in my Preview in the maintenance repo, but I was able to update on my macos device.

                    So, under what diagnostic configuration das this accure? For me, it's by default set to Clang-only checks for almost everything [build-in] and I don't have a warning with the above mentioned code.

                    K Offline
                    K Offline
                    koahnig
                    wrote on 7 Aug 2018, 09:01 last edited by
                    #9

                    @J.Hilk

                    That is basically the set I am using as well. The only difference that I added a setting for avoiding the qDebug() warning issue in release mode encountered with Qt 5.4.

                    Some times you have to wait a moment for warnings to show.

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      koahnig
                      wrote on 7 Aug 2018, 09:13 last edited by koahnig 8 Jul 2018, 09:33
                      #10

                      Basically I was a bit thrown off by the recommendation to use old c-style while in other cases I am receiving warnings when I am using c-style casting. The discussion has been started to back up by bearings. Sometimes one (I) tend to stick to something ineffective.

                      std::string:compare has even in one version the requested substr part integrated.

                      Thanks to all for reponses

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      3

                      1/10

                      5 Aug 2018, 14:07

                      • Login

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