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

Warnings when including QtScript/qscriptengine.h

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Do
    • P3: Somewhat important
    • None
    • 5.12.5
    • Qt Script
    • None
    • Linux/X11

    Description

      Using QtScript with gcc9 with -Wcast-function-type causes compiler warnings when compiling calls to

      template<typename T>
      int qScriptRegisterMetaType(
       QScriptEngine *eng,
       QScriptValue (*toScriptValue)(QScriptEngine *, const T &t),
       void (*fromScriptValue)(const QScriptValue &, T &t),
       const QScriptValue &prototype = QScriptValue(),
       T * /* dummy */ = 0);
      

      The warnings look like this

      In file included from ../../../include/qt5/QtScript/QtScript:10,
       from src/scriptengine.h:9,
       from src/scriptengine.cpp:1:
      ../../../include/qt5/QtScript/qscriptengine.h: In instantiation of 'int qScriptRegisterMetaType(QScriptEngine*, QScriptValue (*)(QScriptEngine*, const T&), void (*)(const QScriptValue&, T&), const QScriptValue&, T*) [with T = QDocumentCursor]':
      src/scriptengine.cpp:454:127: required from here
      ../../../include/qt5/QtScript/qscriptengine.h:387:18: warning: cast between incompatible function types from 'QScriptValue (*)(QScriptEngine*, const QDocumentCursor&)' to 'QScriptEngine::MarshalFunction' \{aka 'QScriptValue (*)(QScriptEngine*, const void*)'} [-Wcast-function-type]
       387 | eng, id, reinterpret_cast<QScriptEngine::MarshalFunction>(toScriptValue),
       | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ../../../include/qt5/QtScript/qscriptengine.h:388:9: warning: cast between incompatible function types from 'void (*)(const QScriptValue&, QDocumentCursor&)' to 'QScriptEngine::DemarshalFunction' \{aka 'void (*)(const QScriptValue&, void*)'} [-Wcast-function-type]
       388 | reinterpret_cast<QScriptEngine::DemarshalFunction>(fromScriptValue),
       | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      

      The warnings are caused by the fact that QtScript does
      reinterpret_cast<QScriptEngine::MarshalFunction>(toScriptValue)
      and
      reinterpret_cast<QScriptEngine::DemarshalFunction>(fromScriptValue)

      The type of toScriptValue is QScriptValue (QScriptEngine *, const T &t)
      and the type of MarshalFunction is QScriptValue (QScriptEngine *, const void *)
      The difference being that one function gets "const T &t" as its last argument and the other gets "const void *"
      The same kind of issue exists with DemarshalFunction

      Qt is able to get away with the reinterpret_cast because most (probably all) C++ compilers implement references internally as pointers, but nothing in the C++ standard guarantees that so it could break at some point. It is probably a good idea to add a bit of extra code and do explicitly the conversion between "const &" and "const void *". If done properly the extra code will be optimized away by the compiler.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            laknoll Lars Knoll
            meansquarederror Vesselin Atanasov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes