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

Inline functions and compilers



    • 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:
    • Platform/s:


      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.


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



            thiago Thiago Macieira
            fanruijie Alan Fan
            0 Vote for this issue
            2 Start watching this issue



                Gerrit Reviews

                There are no open Gerrit changes