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. Trouble assigning value to boolean in the struct
Forum Updated to NodeBB v4.3 + New Features

Trouble assigning value to boolean in the struct

Scheduled Pinned Locked Moved Solved C++ Gurus
32 Posts 7 Posters 4.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.
  • A Offline
    A Offline
    artwaw
    wrote on 24 Jan 2020, 16:39 last edited by
    #1

    Good afternoon,
    I ran into problem I don't understand re the topic.

    I have a struct in a QVector array:

    struct ZFILENAMEPARSER_EXPORT zfpField {
      QString pattern; //!< Pattern used ot initiate QRegularExpression for the field
      bool permanent; //!< is field permanent or can be ommited?
      bool service; //!< is field a service field that should be forwarded to the combobox filtering the view?
      bool skip; //!< should we omit this field in the view?
    };
    

    This vector is a part of larger structure but that's irrelevant here, I think. Through out one of the applications I use I allow the user to set values to different parts of the structure. The trouble is, that I can assign value to the QString field, but can not to the bool. Relevant parts of the code (taken from two different slots of the mainwindow class that handles the view and interaction) below.
    current - the main member holding the whole dataset
    fields - QVector holding an array of zfpField values.

    Now, this one works:

    void MainWindow::patternEditDone() {
      current.fields.value(ui->editSectionNum->currentIndex()).pattern=ui->sectionEdit->text();
    }
    

    and with this one, Qt Creator says "Expression not assignable":

    void MainWindow::permanent(qint32 state) {
      current.fields.value(ui->editSectionNum->currentIndex()).permanent=(state>0);
    }
    

    What have I missed?

    Thanks in advance for the help.

    For more information please re-read.

    Kind Regards,
    Artur

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 24 Jan 2020, 16:59 last edited by Christian Ehrlicher
      #2

      @artwaw said in Trouble assigning value to boolean in the struct:

      Now, this one works:

      It may compile but it for sure does not work as expected: https://doc.qt.io/qt-5/qvector.html#value

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply 24 Jan 2020, 17:06
      4
      • C Christian Ehrlicher
        24 Jan 2020, 16:59

        @artwaw said in Trouble assigning value to boolean in the struct:

        Now, this one works:

        It may compile but it for sure does not work as expected: https://doc.qt.io/qt-5/qvector.html#value

        A Offline
        A Offline
        artwaw
        wrote on 24 Jan 2020, 17:06 last edited by
        #3

        @Christian-Ehrlicher Range of the index is strictly controlled, simply can't be out of bounds. I took care of that.
        My problem is that it does not compile.

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        0
        • H Offline
          H Offline
          hskoglund
          wrote on 24 Jan 2020, 17:17 last edited by hskoglund
          #4

          Hi, perhaps value() is read-only, what happens if you try:

          current.fields[ui->editSectionNum->currentIndex()].permanent=(state>0);
          
          A 1 Reply Last reply 24 Jan 2020, 17:37
          3
          • H hskoglund
            24 Jan 2020, 17:17

            Hi, perhaps value() is read-only, what happens if you try:

            current.fields[ui->editSectionNum->currentIndex()].permanent=(state>0);
            
            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 24 Jan 2020, 17:37 last edited by
            #5

            @hskoglund

            Hi, perhaps value() is read-only

            Not perhaps, it is const.

            That's what Christian wanted to say with his link.

            Regards

            Qt has to stay free or it will die.

            1 Reply Last reply
            1
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 24 Jan 2020, 17:46 last edited by
              #6

              @hskoglund said in Trouble assigning value to boolean in the struct:

              perhaps value() is read-only

              It's even worse - it returns a copy.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • J Offline
                J Offline
                JonB
                wrote on 24 Jan 2020, 21:38 last edited by JonB
                #7

                @artwaw said in Trouble assigning value to boolean in the struct:
                If const is the cause of the error (and I don't doubt it is) for

                current.fields.value(ui->editSectionNum->currentIndex()).permanent=(state>0);
                

                why is there no error for

                current.fields.value(ui->editSectionNum->currentIndex()).pattern=ui->sectionEdit->text();
                

                ? That was the OP's query. That's what I don't see, it should produce the same error.

                1 Reply Last reply
                1
                • H Offline
                  H Offline
                  hskoglund
                  wrote on 24 Jan 2020, 22:07 last edited by
                  #8

                  I suspect it's because the const is only 1 layer deep, i.e. it protects integers and booleans in that struct from being reassigned, but since QStrings are pointers, that const only protects reassignment of the pointer, not what the pointer points to, i.e. QStrings data().

                  1 Reply Last reply
                  0
                  • C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 24 Jan 2020, 22:07 last edited by Christian Ehrlicher
                    #9

                    @JonB said in Trouble assigning value to boolean in the struct:

                    it should produce the same error.

                    In theory it's a temporary so it should compile. In practice the compiler prevents you from shooting into your foot - at least when the left hand side is a POD. As soon as it's a class the compiler will not complain - blame the compiler :)

                    struct blub
                    {
                        int a = 0;
                        int b = 0;
                        blub(int _a, int _b) : a(_a), b(_b) {}
                        blub() = default;
                    };
                    struct foo
                    {
                        std::string stdStr;
                        QString str;
                        QPointF p;
                        blub myBlub;
                        int myInt = -1;
                        bool myBool = false;
                    };
                    
                    QVector<foo> fooVec;
                    
                    void doSomething()
                    {
                        int i = 5;
                        fooVec.value(i).stdStr = "str";
                        fooVec.value(i).str = "str";
                        fooVec.value(i).p = QPointF(1,2);
                        fooVec.value(i).myBlub = blub(1,2);
                        fooVec.value(i).myInt = 2;  // gives compiler error
                        fooVec.value(i).myBool = true;  // dito
                    }
                    

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    K 1 Reply Last reply 24 Jan 2020, 22:13
                    3
                    • C Online
                      C Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 24 Jan 2020, 22:08 last edited by
                      #10

                      @hskoglund said in Trouble assigning value to boolean in the struct:

                      it's because the const

                      QVector::value() does not return a const object!

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      H 1 Reply Last reply 24 Jan 2020, 22:11
                      0
                      • C Christian Ehrlicher
                        24 Jan 2020, 22:08

                        @hskoglund said in Trouble assigning value to boolean in the struct:

                        it's because the const

                        QVector::value() does not return a const object!

                        H Offline
                        H Offline
                        hskoglund
                        wrote on 24 Jan 2020, 22:11 last edited by
                        #11

                        @Christian-Ehrlicher Hmmm, but the docs says that API is const:

                        T QVector::value(int i) const
                        
                        K 1 Reply Last reply 24 Jan 2020, 22:14
                        0
                        • C Christian Ehrlicher
                          24 Jan 2020, 22:07

                          @JonB said in Trouble assigning value to boolean in the struct:

                          it should produce the same error.

                          In theory it's a temporary so it should compile. In practice the compiler prevents you from shooting into your foot - at least when the left hand side is a POD. As soon as it's a class the compiler will not complain - blame the compiler :)

                          struct blub
                          {
                              int a = 0;
                              int b = 0;
                              blub(int _a, int _b) : a(_a), b(_b) {}
                              blub() = default;
                          };
                          struct foo
                          {
                              std::string stdStr;
                              QString str;
                              QPointF p;
                              blub myBlub;
                              int myInt = -1;
                              bool myBool = false;
                          };
                          
                          QVector<foo> fooVec;
                          
                          void doSomething()
                          {
                              int i = 5;
                              fooVec.value(i).stdStr = "str";
                              fooVec.value(i).str = "str";
                              fooVec.value(i).p = QPointF(1,2);
                              fooVec.value(i).myBlub = blub(1,2);
                              fooVec.value(i).myInt = 2;  // gives compiler error
                              fooVec.value(i).myBool = true;  // dito
                          }
                          
                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 24 Jan 2020, 22:13 last edited by kshegunov
                          #12

                          @Christian-Ehrlicher said in Trouble assigning value to boolean in the struct:

                          As soon as it's a class the compiler will not complain - blame the compiler

                          But don't rush to. The compiler has to generate the assignment for the trivial bool, hence it can see that the written is nonsense. On the other hand QString has both assignment operators defined already, so the compiler has to be really, really, really clever to understand that the written assignment is not what was intended. (for example the compiler has to verify that the destructor of mentioned temporary produces no side effects, which isn't trivial to begin with, to be able to then to infer that the assignment is wrong)

                          Read and abide by the Qt Code of Conduct

                          C 1 Reply Last reply 24 Jan 2020, 22:19
                          0
                          • H hskoglund
                            24 Jan 2020, 22:11

                            @Christian-Ehrlicher Hmmm, but the docs says that API is const:

                            T QVector::value(int i) const
                            
                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 24 Jan 2020, 22:14 last edited by
                            #13

                            @hskoglund said in Trouble assigning value to boolean in the struct:

                            but the docs says that API is const

                            The point, Henry, is not that the method is non-mutating, which is without a doubt correct, but that it returns a copy of the element, which copy is then modified.

                            Read and abide by the Qt Code of Conduct

                            H 1 Reply Last reply 24 Jan 2020, 22:17
                            3
                            • K kshegunov
                              24 Jan 2020, 22:14

                              @hskoglund said in Trouble assigning value to boolean in the struct:

                              but the docs says that API is const

                              The point, Henry, is not that the method is non-mutating, which is without a doubt correct, but that it returns a copy of the element, which copy is then modified.

                              H Offline
                              H Offline
                              hskoglund
                              wrote on 24 Jan 2020, 22:17 last edited by
                              #14

                              @kshegunov Aha, you're right! If the method had a const at the beginning things would be different :-)

                              C 1 Reply Last reply 24 Jan 2020, 22:21
                              4
                              • K kshegunov
                                24 Jan 2020, 22:13

                                @Christian-Ehrlicher said in Trouble assigning value to boolean in the struct:

                                As soon as it's a class the compiler will not complain - blame the compiler

                                But don't rush to. The compiler has to generate the assignment for the trivial bool, hence it can see that the written is nonsense. On the other hand QString has both assignment operators defined already, so the compiler has to be really, really, really clever to understand that the written assignment is not what was intended. (for example the compiler has to verify that the destructor of mentioned temporary produces no side effects, which isn't trivial to begin with, to be able to then to infer that the assignment is wrong)

                                C Online
                                C Online
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 24 Jan 2020, 22:19 last edited by
                                #15

                                @kshegunov I thought this too, but even when the compiler generates all the operators (see my struct blub) it will not complain. So I think they simply did not implement it for classes.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                K 1 Reply Last reply 24 Jan 2020, 22:20
                                0
                                • C Christian Ehrlicher
                                  24 Jan 2020, 22:19

                                  @kshegunov I thought this too, but even when the compiler generates all the operators (see my struct blub) it will not complain. So I think they simply did not implement it for classes.

                                  K Offline
                                  K Offline
                                  kshegunov
                                  Moderators
                                  wrote on 24 Jan 2020, 22:20 last edited by
                                  #16

                                  @Christian-Ehrlicher said in Trouble assigning value to boolean in the struct:

                                  @kshegunov I thought this too, but even when the compiler generates all the operators (see my struct blub) it will not complain. So I think they simply did not implement it for classes.

                                  Yes probably, and probably because it's nontrivial in the general case. While at the same time for an intrinsic type it's plain and simple to deduce.

                                  Read and abide by the Qt Code of Conduct

                                  1 Reply Last reply
                                  0
                                  • H hskoglund
                                    24 Jan 2020, 22:17

                                    @kshegunov Aha, you're right! If the method had a const at the beginning things would be different :-)

                                    C Online
                                    C Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 24 Jan 2020, 22:21 last edited by Christian Ehrlicher
                                    #17

                                    @hskoglund said in Trouble assigning value to boolean in the struct:

                                    had a const at the beginning

                                    But then the copy elision would not work afair.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    1 Reply Last reply
                                    0
                                    • H Offline
                                      H Offline
                                      hskoglund
                                      wrote on 24 Jan 2020, 22:41 last edited by hskoglund
                                      #18

                                      Thinking about this, if the intent is to modify the elements of the QVector, then using value() is a bad idea since you're working with a copy (even though it happens to work for QStrings in that struct).

                                      But if the API was changed to have another const at the beginning, say:

                                      const T QVector::value(int i) const
                                      

                                      I'm too lazy to test it, but wouldn't that leading const cause compilation errors also for QStrings assignments? If so that would help dispel confusion about the usage of value().

                                      K 1 Reply Last reply 24 Jan 2020, 23:28
                                      1
                                      • H hskoglund
                                        24 Jan 2020, 22:41

                                        Thinking about this, if the intent is to modify the elements of the QVector, then using value() is a bad idea since you're working with a copy (even though it happens to work for QStrings in that struct).

                                        But if the API was changed to have another const at the beginning, say:

                                        const T QVector::value(int i) const
                                        

                                        I'm too lazy to test it, but wouldn't that leading const cause compilation errors also for QStrings assignments? If so that would help dispel confusion about the usage of value().

                                        K Offline
                                        K Offline
                                        kshegunov
                                        Moderators
                                        wrote on 24 Jan 2020, 23:28 last edited by
                                        #19

                                        @hskoglund said in Trouble assigning value to boolean in the struct:

                                        I'm too lazy to test it, but wouldn't that leading const cause compilation errors also for QStrings assignments?

                                        It would. I'm not sure if the compiler can RVO it correctly if you assign to a non-const however ... that needs actual looking at the asm output.

                                        Read and abide by the Qt Code of Conduct

                                        1 Reply Last reply
                                        1
                                        • C Online
                                          C Online
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on 25 Jan 2020, 07:26 last edited by
                                          #20

                                          From my pov QVector::value() should be deprecated - it's a really weird function which returns a copy just for the reason that the caller does not do the bounds checking.
                                          I don't see a difference in the asm output in a single testcase.

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          1 Reply Last reply
                                          1

                                          4/32

                                          24 Jan 2020, 17:17

                                          28 unread
                                          • Login

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