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. Number Type Cast
QtWS25 Last Chance

Number Type Cast

Scheduled Pinned Locked Moved Solved C++ Gurus
21 Posts 10 Posters 7.5k Views
  • 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.
  • B Offline
    B Offline
    beecksche
    wrote on 7 Mar 2019, 09:41 last edited by
    #1

    Hi,

    I often need to convert/cast integer variables to double for multiplication/division:

    int a = 10;
    int b = 3;
    
    double c = static_cast<double>(a) / b;
    

    In general I use a static_cast, but I also think this is quite ugly to read. To convert the type there are of course multiple ways:

    int a = 10;
    int b = 3;
    
    double c = static_cast<double>(a) / b;
    double d = double(a) / b;
    double e = (double)a / b;
    

    I think the best for reading the code is double d = double(a) / b;. But I don't know if there's a difference and when to use which.

    J J 2 Replies Last reply 7 Mar 2019, 11:37
    0
    • B beecksche
      7 Mar 2019, 09:41

      Hi,

      I often need to convert/cast integer variables to double for multiplication/division:

      int a = 10;
      int b = 3;
      
      double c = static_cast<double>(a) / b;
      

      In general I use a static_cast, but I also think this is quite ugly to read. To convert the type there are of course multiple ways:

      int a = 10;
      int b = 3;
      
      double c = static_cast<double>(a) / b;
      double d = double(a) / b;
      double e = (double)a / b;
      

      I think the best for reading the code is double d = double(a) / b;. But I don't know if there's a difference and when to use which.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 7 Mar 2019, 11:37 last edited by
      #2

      @beecksche In C++ you should use static_cast and avoid C casts in general.
      See https://www.quora.com/What-are-the-disadvantages-of-C-style-casting-in-C++

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

      B 1 Reply Last reply 8 Apr 2019, 05:26
      4
      • B beecksche
        7 Mar 2019, 09:41

        Hi,

        I often need to convert/cast integer variables to double for multiplication/division:

        int a = 10;
        int b = 3;
        
        double c = static_cast<double>(a) / b;
        

        In general I use a static_cast, but I also think this is quite ugly to read. To convert the type there are of course multiple ways:

        int a = 10;
        int b = 3;
        
        double c = static_cast<double>(a) / b;
        double d = double(a) / b;
        double e = (double)a / b;
        

        I think the best for reading the code is double d = double(a) / b;. But I don't know if there's a difference and when to use which.

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 7 Mar 2019, 11:50 last edited by
        #3

        @jsulm is of course correct

        but to add my 2 cents,

        double d = double(a) / b;

        is technically not a cast, if memory serves me right.


        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 1 Reply Last reply 7 Mar 2019, 12:10
        2
        • J J.Hilk
          7 Mar 2019, 11:50

          @jsulm is of course correct

          but to add my 2 cents,

          double d = double(a) / b;

          is technically not a cast, if memory serves me right.

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 7 Mar 2019, 12:10 last edited by aha_1980 3 Jul 2019, 19:49
          #4

          @J.Hilk you are right, it's kind of constructor constructor style cast. But it's C++ and less ugly than static_cast. Qt and me prefer this for primitive types, i.e. int, float etc.

          [Edit aha_1980: incorporated comments from kshegunov]

          Qt has to stay free or it will die.

          K 1 Reply Last reply 7 Mar 2019, 19:45
          2
          • A aha_1980
            7 Mar 2019, 12:10

            @J.Hilk you are right, it's kind of constructor constructor style cast. But it's C++ and less ugly than static_cast. Qt and me prefer this for primitive types, i.e. int, float etc.

            [Edit aha_1980: incorporated comments from kshegunov]

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 7 Mar 2019, 19:45 last edited by
            #5

            @aha_1980 said in Number Type Cast:

            @J.Hilk you are right, it's kind of constructor.

            There are no constructors for primitive types, thus it's no constructor. The "constructor-style cast", which is what you refer to, is of course the same as every other option performance-wise, so it's a matter of aesthetic preference.

            Read and abide by the Qt Code of Conduct

            A 1 Reply Last reply 7 Mar 2019, 19:48
            4
            • K kshegunov
              7 Mar 2019, 19:45

              @aha_1980 said in Number Type Cast:

              @J.Hilk you are right, it's kind of constructor.

              There are no constructors for primitive types, thus it's no constructor. The "constructor-style cast", which is what you refer to, is of course the same as every other option performance-wise, so it's a matter of aesthetic preference.

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 7 Mar 2019, 19:48 last edited by
              #6

              @kshegunov Yeah, you're right, I didn't remember the exact name.

              What I wanted to point out is, that this is C++ and has nothing to do with C casts.

              Qt has to stay free or it will die.

              K 1 Reply Last reply 7 Mar 2019, 19:53
              0
              • A aha_1980
                7 Mar 2019, 19:48

                @kshegunov Yeah, you're right, I didn't remember the exact name.

                What I wanted to point out is, that this is C++ and has nothing to do with C casts.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 7 Mar 2019, 19:53 last edited by
                #7

                @aha_1980 said in Number Type Cast:

                What I wanted to point out is, that this is C++ and has nothing to do with C casts.

                The constructor-style cast is completely equivalent to the C-style cast. It happily casts away const for example. So there's nothing special about it semantically. That is to say:

                Type2 x;
                
                Type1 y1 = (Type1) x;
                Type1 y2 = Type1(x);
                

                are absolutely the same from the compiler's point of view.

                Read and abide by the Qt Code of Conduct

                K 1 Reply Last reply 7 Mar 2019, 20:19
                2
                • K kshegunov
                  7 Mar 2019, 19:53

                  @aha_1980 said in Number Type Cast:

                  What I wanted to point out is, that this is C++ and has nothing to do with C casts.

                  The constructor-style cast is completely equivalent to the C-style cast. It happily casts away const for example. So there's nothing special about it semantically. That is to say:

                  Type2 x;
                  
                  Type1 y1 = (Type1) x;
                  Type1 y2 = Type1(x);
                  

                  are absolutely the same from the compiler's point of view.

                  K Offline
                  K Offline
                  koahnig
                  wrote on 7 Mar 2019, 20:19 last edited by
                  #8

                  @kshegunov said in Number Type Cast:

                  The constructor-style cast is completely equivalent to the C-style cast. It happily casts away const for example. So there's nothing special about it semantically.

                  May I ask that you elaborate a bit more this statement above. It is not clear to me what you mean with "casts away"

                  Type2 x;
                  
                  Type1 y1 = (Type1) x;
                  

                  MinGW and code model would complain with a warning for the statement above.

                  Type1 y2 = Type1(x);

                  MinGW and code model will not throw a warning at you.

                  Therefore I am using the last one.

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

                  K JonBJ 2 Replies Last reply 7 Mar 2019, 20:21
                  1
                  • K koahnig
                    7 Mar 2019, 20:19

                    @kshegunov said in Number Type Cast:

                    The constructor-style cast is completely equivalent to the C-style cast. It happily casts away const for example. So there's nothing special about it semantically.

                    May I ask that you elaborate a bit more this statement above. It is not clear to me what you mean with "casts away"

                    Type2 x;
                    
                    Type1 y1 = (Type1) x;
                    

                    MinGW and code model would complain with a warning for the statement above.

                    Type1 y2 = Type1(x);

                    MinGW and code model will not throw a warning at you.

                    Therefore I am using the last one.

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 7 Mar 2019, 20:21 last edited by kshegunov 3 Jul 2019, 20:22
                    #9

                    @koahnig said in Number Type Cast:

                    May I ask that you elaborate a bit more this statement above. It is not clear to me what you mean with "casts away"

                    const void * x;
                    int y = int(x);
                    

                    compiles just fine. static_cast on the other hand should deny your stripping away the const qualifier.

                    Read and abide by the Qt Code of Conduct

                    J 1 Reply Last reply 7 Mar 2019, 20:50
                    2
                    • K koahnig
                      7 Mar 2019, 20:19

                      @kshegunov said in Number Type Cast:

                      The constructor-style cast is completely equivalent to the C-style cast. It happily casts away const for example. So there's nothing special about it semantically.

                      May I ask that you elaborate a bit more this statement above. It is not clear to me what you mean with "casts away"

                      Type2 x;
                      
                      Type1 y1 = (Type1) x;
                      

                      MinGW and code model would complain with a warning for the statement above.

                      Type1 y2 = Type1(x);

                      MinGW and code model will not throw a warning at you.

                      Therefore I am using the last one.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 7 Mar 2019, 20:41 last edited by
                      #10

                      @koahnig
                      I'm surprised at what you say about MinGW warning for one and not the other. I've looked around the web and do not find anywhere mentioning possibly different compiler warning behaviour. I'd have expected to see it mentioned in e.g. https://stackoverflow.com/questions/4474933/what-exactly-is-or-was-the-purpose-of-c-function-style-casts or https://stackoverflow.com/questions/45505861/function-style-cast-vs-constructor.

                      K 1 Reply Last reply 7 Mar 2019, 20:58
                      0
                      • K kshegunov
                        7 Mar 2019, 20:21

                        @koahnig said in Number Type Cast:

                        May I ask that you elaborate a bit more this statement above. It is not clear to me what you mean with "casts away"

                        const void * x;
                        int y = int(x);
                        

                        compiles just fine. static_cast on the other hand should deny your stripping away the const qualifier.

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 7 Mar 2019, 20:50 last edited by
                        #11

                        @kshegunov said in Number Type Cast:

                        const void * x;
                        int y = int(x);
                        

                        compiles just fine. static_cast on the other hand should deny your stripping away the const qualifier.

                        actually no,
                        as int my be a smaller type than a pointer ;-)
                        But, of course with int64_t it works fine.

                        But I'm nitpicking ..

                        Don't we have const_cast for the above mentioned cases ?


                        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 Mar 2019, 23:30
                        0
                        • JonBJ JonB
                          7 Mar 2019, 20:41

                          @koahnig
                          I'm surprised at what you say about MinGW warning for one and not the other. I've looked around the web and do not find anywhere mentioning possibly different compiler warning behaviour. I'd have expected to see it mentioned in e.g. https://stackoverflow.com/questions/4474933/what-exactly-is-or-was-the-purpose-of-c-function-style-casts or https://stackoverflow.com/questions/45505861/function-style-cast-vs-constructor.

                          K Offline
                          K Offline
                          koahnig
                          wrote on 7 Mar 2019, 20:58 last edited by
                          #12

                          @JonB

                          0_1551991918704_8e8a8bb4-6da9-47fe-82ad-b84b75de805d-image.png

                          OK, I should have the "IIRC" there as I had before. Apparently current versions of MinGW do not.

                          Anyway, I tend to get rid of warnings as much as possible. Mainly because I left warnings in the far past and a missed warning caught me later on.

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

                          1 Reply Last reply
                          4
                          • J J.Hilk
                            7 Mar 2019, 20:50

                            @kshegunov said in Number Type Cast:

                            const void * x;
                            int y = int(x);
                            

                            compiles just fine. static_cast on the other hand should deny your stripping away the const qualifier.

                            actually no,
                            as int my be a smaller type than a pointer ;-)
                            But, of course with int64_t it works fine.

                            But I'm nitpicking ..

                            Don't we have const_cast for the above mentioned cases ?

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 7 Mar 2019, 23:30 last edited by kshegunov 3 Jul 2019, 23:32
                            #13

                            @J.Hilk said in Number Type Cast:

                            actually no,

                            Actually yes. Almost all errors that fall in the -fpermissive are there to prevent you from shooting yourself in the foot. The compiler can compile it just fine, hence the existence of the aforementioned flag. As a matter of fact if you try it with MSVC you're (probably with surprise) going to realize that it eats it just fine - no errors, just a warning or so.

                            as int my be a smaller type than a pointer ;-)

                            So? Substitute void * with long long and see what happens ... pure magic ... Truncation is unimportant here, and truncation is not an error by the way.

                            Don't we have const_cast for the above mentioned cases ?

                            Indeed, and some compilers may just allow you to static_cast it too depending on the types and how the compiler's implemented. However, here's a better snippet for your consideration:

                            typedef void* voidp;
                            
                            const int * x;
                            void * y = static_cast<voidp>(x); // Case 1
                            void * z = voidp(x);              // Case 2
                            

                            Case 1 is an error, because you're trying to const_cast and static_cast at the same time, Case 2 is valid and compiles fine. Most of the time this is harmless, obviously, as with the case of primitive types, but, and I repeat again, there's no semantic difference between C-style casts and constructor-style casts; they are exactly the same thing.

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            5
                            • Kent-DorfmanK Offline
                              Kent-DorfmanK Offline
                              Kent-Dorfman
                              wrote on 8 Mar 2019, 06:53 last edited by Kent-Dorfman 3 Aug 2019, 06:56
                              #14

                              As I often need to code in several different languages I try to make my expressions as language agnostic as possible. Unless performance is a consideration I often do this to force a type conversion:

                              int a(12);
                              int b(15);
                              double d(a / (b + 0.0));
                              

                              in many lanuages there will be an implicit conversion to floating point, and I prefer my conversion in the denominator.

                              JonBJ 1 Reply Last reply 8 Mar 2019, 08:45
                              1
                              • Kent-DorfmanK Kent-Dorfman
                                8 Mar 2019, 06:53

                                As I often need to code in several different languages I try to make my expressions as language agnostic as possible. Unless performance is a consideration I often do this to force a type conversion:

                                int a(12);
                                int b(15);
                                double d(a / (b + 0.0));
                                

                                in many lanuages there will be an implicit conversion to floating point, and I prefer my conversion in the denominator.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on 8 Mar 2019, 08:45 last edited by
                                #15

                                @Kent-Dorfman This is a new one on me --- try to write code in one language so it will also work in another :)

                                1 Reply Last reply
                                0
                                • fcarneyF Offline
                                  fcarneyF Offline
                                  fcarney
                                  wrote on 3 Apr 2019, 22:31 last edited by
                                  #16

                                  I have nothing significant to add to this discussion other than I still do C style casts in my C++ code. I am still not used to using all the new casting templates (they are templates right?).

                                  So whether you use C or C++ casts just Cast Away!

                                  C++ is a perfectly valid school of magic.

                                  K 1 Reply Last reply 3 Apr 2019, 22:44
                                  0
                                  • fcarneyF fcarney
                                    3 Apr 2019, 22:31

                                    I have nothing significant to add to this discussion other than I still do C style casts in my C++ code. I am still not used to using all the new casting templates (they are templates right?).

                                    So whether you use C or C++ casts just Cast Away!

                                    K Offline
                                    K Offline
                                    kshegunov
                                    Moderators
                                    wrote on 3 Apr 2019, 22:44 last edited by
                                    #17

                                    @fcarney said in Number Type Cast:

                                    they are templates right?

                                    No, they're operators.

                                    Read and abide by the Qt Code of Conduct

                                    1 Reply Last reply
                                    3
                                    • J jsulm
                                      7 Mar 2019, 11:37

                                      @beecksche In C++ you should use static_cast and avoid C casts in general.
                                      See https://www.quora.com/What-are-the-disadvantages-of-C-style-casting-in-C++

                                      B Offline
                                      B Offline
                                      beecksche
                                      wrote on 8 Apr 2019, 05:26 last edited by
                                      #18

                                      Really interesting disucssion. I'm with @jsulm and use the "constructor-style cast" for primitives types.

                                      int a = 10;
                                      double b = double(a);
                                      

                                      And for safety reasons *_cast for classes

                                      MyBaseClass *base;
                                      
                                      MyDerivedClass *derived = static_cast<MyDerivedClass*>(base);
                                      
                                      J 1 Reply Last reply 8 Apr 2019, 05:31
                                      1
                                      • B beecksche
                                        8 Apr 2019, 05:26

                                        Really interesting disucssion. I'm with @jsulm and use the "constructor-style cast" for primitives types.

                                        int a = 10;
                                        double b = double(a);
                                        

                                        And for safety reasons *_cast for classes

                                        MyBaseClass *base;
                                        
                                        MyDerivedClass *derived = static_cast<MyDerivedClass*>(base);
                                        
                                        J Offline
                                        J Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on 8 Apr 2019, 05:31 last edited by
                                        #19

                                        @beecksche I actually wrote that C style casts should be avoided in general (means: for all types including primitive types).

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

                                        1 Reply Last reply
                                        1
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 8 Apr 2019, 07:19 last edited by
                                          #20

                                          @beecksche said in Number Type Cast:

                                          MyDerivedClass derived = static_cast<MyDerivedClass>(base);

                                          Just one note: static_cast has absolutely no notion of safety. There's no check involved so you are completely responsible to ensure that the type you are casting to is really is valid with regard to the type you are casting from.

                                          dynamic_cast does check and in the case of pointers return a null pointer that you can validate before continuing your code.

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          B 1 Reply Last reply 9 Apr 2019, 03:15
                                          4

                                          • Login

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