Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-96199

Inline functions and compilers

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Not Evaluated
    • Resolution: Invalid
    • Affects Version/s: 5.11.3, 5.15.1
    • Fix Version/s: None
    • Component/s: Core: Other
    • Labels:
      None
    • Platform/s:
      Linux/X11

      Description

      In this change: https://codereview.qt-project.org/c/qt/qtbase/+/305554/4, I found a bug.

      When I merge the changes here into Qt5, the program cannot run. The error information is shown in the screenshot below. I think this is caused by inline functions.

      The original code is like this:

      const int nxp = qRound(xp);
      const int nyp = qRound(yp);
      const int nw = qRound(w + (xp - nxp)/2);
      const int nh = qRound(h + (yp - nyp)/2);
      return QRect(nxp, nyp, nw, nh);
      

      If I change it to this, the program can run well: 

      return QRect(qRound(xp), qRound(yp), qRound(w + (xp - qRound(xp))/2), qRound(h + (yp - qRound(yp))/2));

      In C++, the concept of predefined macro is realized by inline function, which is also an actual function. Inline functions have all the behaviors of normal functions. The only difference is that inline functions expand like predefined macros, so there is no cost of function calls. Therefore, we should use inline functions instead of macros normally. However, it is well known that inline functions are not always effective everywhere. There are some limitations in C + + inline compilation. The compiler may not consider inline compilation of functions in the following cases:

      • There cannot be any form of circular statement
      • There cannot be too many conditional statements
      • The function body cannot be too large
      • Function cannot be addressed

      Inline is just a suggestion to the compiler. The compiler does not necessarily accept this suggestion. If you do not declare a function as an inline function, the compiler may also compile this function inline. A good compiler will inline small, simple functions.

        Attachments

        1. Demo.zip
          2 kB
        2. screenshot.png
          screenshot.png
          389 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            thiago Thiago Macieira
            Reporter:
            fanruijie Alan Fan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes