Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Avoiding warnings about int <=> qsizetype on Windows with MSVC
Forum Updated to NodeBB v4.3 + New Features

Avoiding warnings about int <=> qsizetype on Windows with MSVC

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 1.4k Views 1 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.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by Robert Hairgrove
    #1

    I am trying to set up my code base so that it will compile both with Qt 5.15.5 and Qt 6.5.3 both on Linux with GCC and Windows using MSVC 2019.

    On Linux Ubuntu, there are never any warnings issued concerning narrowing conversions with the default compiler settings, e.g. when returning an int from a function that calls a Qt object's size() function (as of Qt 5.10, the type returned is qsizetype).

    MSVC issues the C4244 warning about possible loss of data since qsizetype is larger than a 32-bit int.

    Unfortunately, there are very many places where I need to update the code since I do not want to globally surpress this warning.

    Can anyone give me some tips on how to "ease the pain" here? I already saw the tip to use auto, but #ifdef qsizetype doesn't seem to work since it is a typedef and not a macro. I'm not sure that auto will work in a header file as the return value from a function.

    KenAppleby 0K 1 Reply Last reply
    0
    • R Robert Hairgrove

      I am trying to set up my code base so that it will compile both with Qt 5.15.5 and Qt 6.5.3 both on Linux with GCC and Windows using MSVC 2019.

      On Linux Ubuntu, there are never any warnings issued concerning narrowing conversions with the default compiler settings, e.g. when returning an int from a function that calls a Qt object's size() function (as of Qt 5.10, the type returned is qsizetype).

      MSVC issues the C4244 warning about possible loss of data since qsizetype is larger than a 32-bit int.

      Unfortunately, there are very many places where I need to update the code since I do not want to globally surpress this warning.

      Can anyone give me some tips on how to "ease the pain" here? I already saw the tip to use auto, but #ifdef qsizetype doesn't seem to work since it is a typedef and not a macro. I'm not sure that auto will work in a header file as the return value from a function.

      KenAppleby 0K Offline
      KenAppleby 0K Offline
      KenAppleby 0
      wrote on last edited by KenAppleby 0
      #2

      @Robert-Hairgrove
      The least painful way I have found is

      int n(list.size());
      

      which quietly does the static_cast, whereas

      int n{ list.size() };
      

      issues the loss of data warning, and clang raises a semantic issue.

      int n = list.size();
      

      Issues no warnings;
      This is using MinGW 64, but I expect this would be the same on MSVC.

      I am confident none of my containers exceed 2'147'483'648 in length, at least not intentionally!

      R 1 Reply Last reply
      0
      • KenAppleby 0K KenAppleby 0

        @Robert-Hairgrove
        The least painful way I have found is

        int n(list.size());
        

        which quietly does the static_cast, whereas

        int n{ list.size() };
        

        issues the loss of data warning, and clang raises a semantic issue.

        int n = list.size();
        

        Issues no warnings;
        This is using MinGW 64, but I expect this would be the same on MSVC.

        I am confident none of my containers exceed 2'147'483'648 in length, at least not intentionally!

        R Offline
        R Offline
        Robert Hairgrove
        wrote on last edited by
        #3

        @KenAppleby-0 said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

        int n = list.size();

        Issues no warnings;
        This is using MinGW 64, but I expect this would be the same on MSVC.

        MSVC warns C4244 about possible loss of data here. If I do a C-style cast, as your first suggestion does, it issues no warning.

        Thank you for that!

        KenAppleby 0K 1 Reply Last reply
        0
        • R Robert Hairgrove has marked this topic as solved on
        • R Robert Hairgrove

          @KenAppleby-0 said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

          int n = list.size();

          Issues no warnings;
          This is using MinGW 64, but I expect this would be the same on MSVC.

          MSVC warns C4244 about possible loss of data here. If I do a C-style cast, as your first suggestion does, it issues no warning.

          Thank you for that!

          KenAppleby 0K Offline
          KenAppleby 0K Offline
          KenAppleby 0
          wrote on last edited by
          #4

          @Robert-Hairgrove said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

          If I do a C-style cast, as your first suggestion does

          It's not a C-style cast, as I understand it. That would be

          int n = (int)list.size();
          

          or

          int n{ (int)list.size() };
          

          It's more an implicit static_cast. C-style casts are forbidden where I come from! :-)

          J.HilkJ 1 Reply Last reply
          0
          • KenAppleby 0K KenAppleby 0

            @Robert-Hairgrove said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

            If I do a C-style cast, as your first suggestion does

            It's not a C-style cast, as I understand it. That would be

            int n = (int)list.size();
            

            or

            int n{ (int)list.size() };
            

            It's more an implicit static_cast. C-style casts are forbidden where I come from! :-)

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @KenAppleby-0 said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

            C-style casts are forbidden where I come from! :-)

            away to jail with you than!
            int y = (int)x; and int y = int(x); are functionally the same in C++. Both are examples of explicit type casting, where the value of x is converted to an int before being assigned to y.
            However, the syntax int y = int(x); is known as a functional cast, which is more similar to the syntax used by constructors in object-oriented programming. Despite the syntactic differences, both forms will perform the same type conversion operation in this context.


            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.

            KenAppleby 0K 2 Replies Last reply
            2
            • J.HilkJ J.Hilk

              @KenAppleby-0 said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

              C-style casts are forbidden where I come from! :-)

              away to jail with you than!
              int y = (int)x; and int y = int(x); are functionally the same in C++. Both are examples of explicit type casting, where the value of x is converted to an int before being assigned to y.
              However, the syntax int y = int(x); is known as a functional cast, which is more similar to the syntax used by constructors in object-oriented programming. Despite the syntactic differences, both forms will perform the same type conversion operation in this context.

              KenAppleby 0K Offline
              KenAppleby 0K Offline
              KenAppleby 0
              wrote on last edited by
              #6

              @J-Hilk said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

              int y = (int)x; and int y = int(x); are functionally the same in C++. Both are examples of explicit type casting, where the value of x is converted to an int before being assigned to y

              Functional equivalence in one special case is not identity in general, or even in that special case.
              On a 64-bit machine with a C-style cast you can do:

              const uint64_t a{ 0 };
              const void * b = (void *)a;
              

              or even

              const Foo * foo = (Foo *) a;
              

              without getting even a warning. And you can't do

              const void * c = void *(a);
              

              "The C-style cast is far more dangerous than the named conversion operators because the notation is harder to spot in a large program and the kind of conversion intended ... is not explicit." Stroustrup.

              I'm with Bjarne. :-)

              JonBJ 1 Reply Last reply
              0
              • KenAppleby 0K KenAppleby 0

                @J-Hilk said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

                int y = (int)x; and int y = int(x); are functionally the same in C++. Both are examples of explicit type casting, where the value of x is converted to an int before being assigned to y

                Functional equivalence in one special case is not identity in general, or even in that special case.
                On a 64-bit machine with a C-style cast you can do:

                const uint64_t a{ 0 };
                const void * b = (void *)a;
                

                or even

                const Foo * foo = (Foo *) a;
                

                without getting even a warning. And you can't do

                const void * c = void *(a);
                

                "The C-style cast is far more dangerous than the named conversion operators because the notation is harder to spot in a large program and the kind of conversion intended ... is not explicit." Stroustrup.

                I'm with Bjarne. :-)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @KenAppleby-0 said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

                Functional equivalence in one special case is not identity in general, or even in that special case.

                I don't see @J-Hilk said anything about this. He said:

                int y = (int)x; and int y = int(x); are functionally the same in C++

                which they are. I don't read that as saying there is nothing you can do with explicit casts that cannot be done with functional casts.

                And you can't do
                const void * c = void *(a);

                typedef void *voidp;
                const voidp c = voidp(a);
                

                Is that what you are alluding to? That's why @J-Hilk said

                Despite the syntactic differences

                KenAppleby 0K 1 Reply Last reply
                0
                • JonBJ JonB

                  @KenAppleby-0 said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

                  Functional equivalence in one special case is not identity in general, or even in that special case.

                  I don't see @J-Hilk said anything about this. He said:

                  int y = (int)x; and int y = int(x); are functionally the same in C++

                  which they are. I don't read that as saying there is nothing you can do with explicit casts that cannot be done with functional casts.

                  And you can't do
                  const void * c = void *(a);

                  typedef void *voidp;
                  const voidp c = voidp(a);
                  

                  Is that what you are alluding to? That's why @J-Hilk said

                  Despite the syntactic differences

                  KenAppleby 0K Offline
                  KenAppleby 0K Offline
                  KenAppleby 0
                  wrote on last edited by
                  #8

                  Functional equivalence in one special case is not identity in general, or even in that special case.

                  @JonB said

                  I don't see @J-Hilk said anything about this. He said:

                  int y = (int)x; and int y = int(x); are functionally the same in C++

                  That's not what my comment says. @J-Hilk is saying that

                  int y = int(x);
                  

                  is a C-style cast. I am saying it isn't. It may do the same thing, at the compiler's discretion, but it isn't a C-style cast. One of the reasons that it isn't is syntactical.

                  jsulmJ 1 Reply Last reply
                  0
                  • KenAppleby 0K KenAppleby 0

                    Functional equivalence in one special case is not identity in general, or even in that special case.

                    @JonB said

                    I don't see @J-Hilk said anything about this. He said:

                    int y = (int)x; and int y = int(x); are functionally the same in C++

                    That's not what my comment says. @J-Hilk is saying that

                    int y = int(x);
                    

                    is a C-style cast. I am saying it isn't. It may do the same thing, at the compiler's discretion, but it isn't a C-style cast. One of the reasons that it isn't is syntactical.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @KenAppleby-0 said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

                    . @J-Hilk is saying that
                    int y = int(x);

                    is a C-style cast.

                    Where? He wrote: "int y = (int)x; and int y = int(x); are functionally the same in C++".

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

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

                      @KenAppleby-0 said in Avoiding warnings about int <=> qsizetype on Windows with MSVC:

                      C-style casts are forbidden where I come from! :-)

                      away to jail with you than!
                      int y = (int)x; and int y = int(x); are functionally the same in C++. Both are examples of explicit type casting, where the value of x is converted to an int before being assigned to y.
                      However, the syntax int y = int(x); is known as a functional cast, which is more similar to the syntax used by constructors in object-oriented programming. Despite the syntactic differences, both forms will perform the same type conversion operation in this context.

                      KenAppleby 0K Offline
                      KenAppleby 0K Offline
                      KenAppleby 0
                      wrote on last edited by
                      #10

                      @jsulm

                      Where?

                      Here: I had written

                      C-style casts are forbidden where I come from! :-)

                      he wrote

                      away to jail with you than!(sic)
                      int y = (int)x; and int y = int(x); are functionally the same in C++

                      I had not written "anything functionally the same as a C-style cast is forbidden" [which would forbid all named casts, so I'm unlikely to say that.]

                      The "away to jail" jibe makes sense only if it is asserting that int(x) isa C-style cast.

                      Enough.

                      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