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. Conditional operator as a statement
Forum Updated to NodeBB v4.3 + New Features

Conditional operator as a statement

Scheduled Pinned Locked Moved Unsolved C++ Gurus
10 Posts 6 Posters 122 Views 3 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote last edited by
    #1

    In a post just made by someone here they use the following as a statement:

    (docType == SW::FACTURA) ? setLabelText("Factura") : setLabelText("Boleta");
    

    I have not tried this in any compilers to see if they have anything to say, and I do not know which compiler the OP is using. But:

    • How "OK" or "not OK" is this regarded by the C++ community? I would never use it as a statement, and I do not recall ever seeing it used as such in any C++ code I have seen over the years.

    • Furthermore, I know compilers demand that each side of the : return the same type in a conditional operator. But here setLabelText() is (presumably) void, isn't that a return type which is not acceptable in a ? : ?

    P.S.
    Before anyone says, of course here I would write this as

     setLabelText(docType == SW::FACTURA ? "Factura" : "Boleta");
    

    but that is not the point of my question. The OP could also have had different function calls in the statement, like:

    (a == b) ? someFunc() : otherFunc();
    
    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote last edited by
      #2

      If there's a doubt there's no doubt.

      That's a weird line of code that makes you double check it to be sure you understood correctly, thus it should be avoided.

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote last edited by
        #3

        I saw this idiom some times but not often. It's not wrong but not very common.

        btw: I've a similar statement in python where I sa a c++ programmer always have to thunk about it: a = 3 if b == 4 else 5

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

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          I saw this idiom some times but not often. It's not wrong but not very common.

          btw: I've a similar statement in python where I sa a c++ programmer always have to thunk about it: a = 3 if b == 4 else 5

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote last edited by
          #4

          @Christian-Ehrlicher
          I agree I don't like the syntax/ordering/layout of the Python if else operator. For one thing I don't like how your eye has to go to pick out the possible values of 3 or 5 for a, one near the beginning while the other is at the end. The Python statement is the same as the C++ conditional operator written as:

          a = (b == 4) ? 3 : 5;
          

          But you cannot use the Python if else on its own as a complete statement as just

          3 if b == 4 else 5
          someFunction() if b == 4 else anotherFunction()
          

          where we are comparing against the poster who has used C++ ? : as a standalone statement.

          1 Reply Last reply
          0
          • J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote last edited by
            #5

            it's fine, you don't necessarily need to have the same return type either.

            int i = 1;
            double d = 2.5;
            auto x = cond ? i : d;        // -> double 
            

            is totally legit. But it may produce compiler warnings, if the appropriate compiler flag are enabled (-Wconversion, -Wsign-conversion, -Wfloat-conversion etc) and its applicable


            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.

            JonBJ 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              it's fine, you don't necessarily need to have the same return type either.

              int i = 1;
              double d = 2.5;
              auto x = cond ? i : d;        // -> double 
              

              is totally legit. But it may produce compiler warnings, if the appropriate compiler flag are enabled (-Wconversion, -Wsign-conversion, -Wfloat-conversion etc) and its applicable

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote last edited by JonB
              #6

              @J.Hilk said in Conditional operator as a statement:

              is totally legit.

              Only because int is "promotable" to double. They do not have to the same return type but they at least have to have promotable/convertible return types.

              The (academic) question is whether that is enforced by the ? : operator itself or whether it is only enforced on (say) assignment of the result to a variable or usage in a condition. The situation we are discussing is the (peculiar):

              a ? b() : c();
              

              as a statement, so the result is never used. I still imagine it's the : operator which demands the type compatibility even if the result is not used.

              BTW (I just discovered) the "Standard" says something along these lines:

              5.17/3

              If the second and third operand have different types, and either has (possibly cv-qualified) class type, an attempt is made to convert each of those operands to the type of the other. The process for determining whether an operand expression E1 of type T1 can be converted to match an operand expression E2 of type T2 is defined as follows:

              and then, of course, loads of cases.....

              jeremy_kJ 1 Reply Last reply
              1
              • JonBJ JonB

                @J.Hilk said in Conditional operator as a statement:

                is totally legit.

                Only because int is "promotable" to double. They do not have to the same return type but they at least have to have promotable/convertible return types.

                The (academic) question is whether that is enforced by the ? : operator itself or whether it is only enforced on (say) assignment of the result to a variable or usage in a condition. The situation we are discussing is the (peculiar):

                a ? b() : c();
                

                as a statement, so the result is never used. I still imagine it's the : operator which demands the type compatibility even if the result is not used.

                BTW (I just discovered) the "Standard" says something along these lines:

                5.17/3

                If the second and third operand have different types, and either has (possibly cv-qualified) class type, an attempt is made to convert each of those operands to the type of the other. The process for determining whether an operand expression E1 of type T1 can be converted to match an operand expression E2 of type T2 is defined as follows:

                and then, of course, loads of cases.....

                jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote last edited by
                #7

                @JonB I was going to suggest including a link to the relevant standard section, but attempting to do so myself isn't producing satisfactory results.

                https://github.com/cplusplus/draft/blob/main/source/expressions.tex#L7819 for the picky,
                https://en.cppreference.com/w/cpp/language/operator_other.html for the less patient.

                Asking a question about code? http://eel.is/iso-c++/testcase/

                JonBJ 1 Reply Last reply
                1
                • jeremy_kJ jeremy_k

                  @JonB I was going to suggest including a link to the relevant standard section, but attempting to do so myself isn't producing satisfactory results.

                  https://github.com/cplusplus/draft/blob/main/source/expressions.tex#L7819 for the picky,
                  https://en.cppreference.com/w/cpp/language/operator_other.html for the less patient.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote last edited by
                  #8

                  @jeremy_k Thanks. I normally do, this time I actually found that in a long stackoverflow post and just wanted to pick that excerpt.

                  1 Reply Last reply
                  0
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote last edited by
                    #9

                    It is syntactically correct (just an expression of type void), but it's a case of what I would call "clever code" (not a compliment). It lessens readability and kinda hides the real action in a side effect of an operator for the sake of... yeah, being clever I guess, as it's not even shorter or anything.

                    Another example of this I've seen is the "clever" use of comma operator:
                    std::cout << calculate42(), calculate43();
                    Without looking it up - do you remember what gets printed?

                    JonBJ 1 Reply Last reply
                    1
                    • Chris KawaC Chris Kawa

                      It is syntactically correct (just an expression of type void), but it's a case of what I would call "clever code" (not a compliment). It lessens readability and kinda hides the real action in a side effect of an operator for the sake of... yeah, being clever I guess, as it's not even shorter or anything.

                      Another example of this I've seen is the "clever" use of comma operator:
                      std::cout << calculate42(), calculate43();
                      Without looking it up - do you remember what gets printed?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote last edited by JonB
                      #10

                      @Chris-Kawa said in Conditional operator as a statement:

                      Without looking it up - do you remember what gets printed?

                      , operator returns value to right of it, so I would expect to see 1 more than the answer to Life, The Universe & Everything :)

                      You can, of course, also implement simple if then else statements with && and ||:

                      x && xIsTrue();
                      x || xIsFalse();
                      x && xIsTrue() || xIsFalse();
                      
                      1 Reply Last reply
                      0

                      • Login

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