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. QHttpServer: use function pointer as ViewHandler

QHttpServer: use function pointer as ViewHandler

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 1.2k 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.
  • T Offline
    T Offline
    Tarae
    wrote on 30 Mar 2023, 12:32 last edited by
    #1

    Using Qt 6.4.3, playing with QHttpServer.

    Documentation: ViewHandler can be a function pointer, non-mutable lambda...

    But I don't know how to use it with a function pointer:

    #include <QCoreApplication>
    #include <QHttpServer>
    
    const char * responseFunction()
    {
      return "Hello world!";
    }
    
    int main(int argc, char *argv[])
    {
      QCoreApplication a(argc, argv);
    
      QHttpServer server;
    
      //server.route("/", [](){  return "Hello world!";}); // this compiles
      server.route("/", &responseFunction); // this doesn't compile
    
      server.listen(QHostAddress::Any, 2222);
    
      return a.exec();
    }
    

    The program compiles and works when lambda is used. But I can't compile it with a function pointer. What am I doing wrong?

    J J 2 Replies Last reply 30 Mar 2023, 12:34
    0
    • T Tarae
      31 Mar 2023, 13:47

      @Christian-Ehrlicher I've created a bug report: https://bugreports.qt.io/browse/QTBUG-112484

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 25 Apr 2023, 18:34 last edited by
      #19

      And fixed

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • T Tarae
        30 Mar 2023, 12:32

        Using Qt 6.4.3, playing with QHttpServer.

        Documentation: ViewHandler can be a function pointer, non-mutable lambda...

        But I don't know how to use it with a function pointer:

        #include <QCoreApplication>
        #include <QHttpServer>
        
        const char * responseFunction()
        {
          return "Hello world!";
        }
        
        int main(int argc, char *argv[])
        {
          QCoreApplication a(argc, argv);
        
          QHttpServer server;
        
          //server.route("/", [](){  return "Hello world!";}); // this compiles
          server.route("/", &responseFunction); // this doesn't compile
        
          server.listen(QHostAddress::Any, 2222);
        
          return a.exec();
        }
        

        The program compiles and works when lambda is used. But I can't compile it with a function pointer. What am I doing wrong?

        J Offline
        J Offline
        JonB
        wrote on 30 Mar 2023, 12:34 last edited by JonB
        #2

        @Tarae said in QHttpServer: use function pointer as ViewHandler:

        server.route("/", &responseFunction); // this doesn't compile

        Usually I thought it does not matter with C function pointers, but try removing that &. Plain responseFunction is a function pointer. Does that resolve?

        Having said that, I don't think your function is acceptable anyway:

        ViewHandler can be a function pointer, non-mutable lambda, or any other copiable callable with const call operator. The callable can take two optional special arguments: const QHttpServerRequest& and QHttpServerResponder&&. These special arguments must be the last in the parameter list, but in any order, and there can be none, one, or both of them present.

        Oh, no, I see, the parameters are optional anyway.

        T 1 Reply Last reply 30 Mar 2023, 12:41
        0
        • T Tarae
          30 Mar 2023, 12:32

          Using Qt 6.4.3, playing with QHttpServer.

          Documentation: ViewHandler can be a function pointer, non-mutable lambda...

          But I don't know how to use it with a function pointer:

          #include <QCoreApplication>
          #include <QHttpServer>
          
          const char * responseFunction()
          {
            return "Hello world!";
          }
          
          int main(int argc, char *argv[])
          {
            QCoreApplication a(argc, argv);
          
            QHttpServer server;
          
            //server.route("/", [](){  return "Hello world!";}); // this compiles
            server.route("/", &responseFunction); // this doesn't compile
          
            server.listen(QHostAddress::Any, 2222);
          
            return a.exec();
          }
          

          The program compiles and works when lambda is used. But I can't compile it with a function pointer. What am I doing wrong?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 30 Mar 2023, 12:35 last edited by
          #3

          @Tarae And it could also help if you would post the compiler error...

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

          T 1 Reply Last reply 30 Mar 2023, 12:44
          0
          • J JonB
            30 Mar 2023, 12:34

            @Tarae said in QHttpServer: use function pointer as ViewHandler:

            server.route("/", &responseFunction); // this doesn't compile

            Usually I thought it does not matter with C function pointers, but try removing that &. Plain responseFunction is a function pointer. Does that resolve?

            Having said that, I don't think your function is acceptable anyway:

            ViewHandler can be a function pointer, non-mutable lambda, or any other copiable callable with const call operator. The callable can take two optional special arguments: const QHttpServerRequest& and QHttpServerResponder&&. These special arguments must be the last in the parameter list, but in any order, and there can be none, one, or both of them present.

            Oh, no, I see, the parameters are optional anyway.

            T Offline
            T Offline
            Tarae
            wrote on 30 Mar 2023, 12:41 last edited by Tarae
            #4

            @JonB I've tried with &, without & , with * or && or **. Nothing works.

            1 Reply Last reply
            0
            • J jsulm
              30 Mar 2023, 12:35

              @Tarae And it could also help if you would post the compiler error...

              T Offline
              T Offline
              Tarae
              wrote on 30 Mar 2023, 12:44 last edited by
              #5

              @jsulm there is compiler output: https://pastebin.com/NLrxHdTE

              J 1 Reply Last reply 30 Mar 2023, 12:46
              0
              • T Tarae
                30 Mar 2023, 12:44

                @jsulm there is compiler output: https://pastebin.com/NLrxHdTE

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 30 Mar 2023, 12:46 last edited by
                #6

                @Tarae I can't open this URL.
                Please post the error here, it's simple text...

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

                T 1 Reply Last reply 30 Mar 2023, 12:52
                2
                • J jsulm
                  30 Mar 2023, 12:46

                  @Tarae I can't open this URL.
                  Please post the error here, it's simple text...

                  T Offline
                  T Offline
                  Tarae
                  wrote on 30 Mar 2023, 12:52 last edited by Tarae
                  #7

                  @jsulm Ok, here it is:

                  14:38:56: Running steps for project httptest...
                  14:38:56: Configuration unchanged, skipping qmake step.
                  14:38:56: Starting: "/usr/bin/make" -j8
                  /home/user/Qt/6.4.3/gcc_64/bin/qmake -o Makefile ../httptest/httptest.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug
                  g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -fPIC -D_REENTRANT -DQT_QML_DEBUG -DQT_HTTPSERVER_LIB -DQT_WEBSOCKETS_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../httptest -I. -I/home/user/Qt/6.4.3/gcc_64/include -I/home/user/Qt/6.4.3/gcc_64/include/QtHttpServer -I/home/user/Qt/6.4.3/gcc_64/include/QtWebSockets -I/home/user/Qt/6.4.3/gcc_64/include/QtNetwork -I/home/user/Qt/6.4.3/gcc_64/include/QtCore -I. -I/home/user/Qt/6.4.3/gcc_64/mkspecs/linux-g++ -o main.o ../httptest/main.cpp
                  In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:8,
                                   from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:8,
                                   from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:9,
                                   from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                                   from ../httptest/main.cpp:2:
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h: In instantiation of ‘struct QtPrivate::FunctionTraits<const char* (* const&)()>’:
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h:65:11:   required from ‘struct QtPrivate::ViewTraits<const char* (* const&)(), false>’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:18:8:   required from ‘struct QtPrivate::RouterViewTraitsHelper<const char* (* const&)(), false>’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:122:11:   required from ‘struct QHttpServerRouterViewTraits<const char* (* const&)(), false>’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39:   required by substitution of ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const [with ViewHandler = const char* (* const&)(); ViewTraits = QHttpServerRouterViewTraits<const char* (* const&)(), false>]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59:   required from ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
                  ../httptest/main.cpp:16:15:   required from here
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h:42:56: error: ‘operator()’ is not a member of ‘std::remove_cv<const char* (* const)()>::type’ {aka ‘const char* (*)()’}
                     42 | struct FunctionTraits : public FunctionTraits<decltype(&q20::remove_cvref_t<T>::operator())>{};
                        |                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h: In instantiation of ‘struct QtPrivate::ViewTraits<const char* (* const&)(), false>’:
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:18:8:   required from ‘struct QtPrivate::RouterViewTraitsHelper<const char* (* const&)(), false>’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:122:11:   required from ‘struct QHttpServerRouterViewTraits<const char* (* const&)(), false>’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39:   required by substitution of ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const [with ViewHandler = const char* (* const&)(); ViewTraits = QHttpServerRouterViewTraits<const char* (* const&)(), false>]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59:   required from ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
                  ../httptest/main.cpp:16:15:   required from here
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h:65:11: error: ‘ArgumentCount’ is not a member of ‘QtPrivate::FunctionTraits<const char* (* const&)()>’
                     65 |     using ArgumentIndexes = typename Indexes<FTraits::ArgumentCount>::Value;
                        |           ^~~~~~~~~~~~~~~
                  In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:8,
                                   from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:9,
                                   from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                                   from ../httptest/main.cpp:2:
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h: In instantiation of ‘struct QHttpServerRouterViewTraits<const char* (* const&)(), false>’:
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39:   required by substitution of ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const [with ViewHandler = const char* (* const&)(); ViewTraits = QHttpServerRouterViewTraits<const char* (* const&)(), false>]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59:   required from ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
                  ../httptest/main.cpp:16:15:   required from here
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:122:11: error: no type named ‘ReturnType’ in ‘using FunctionTraits = using FTraits = struct QtPrivate::FunctionTraits<const char* (* const&)()>’ {aka ‘struct QtPrivate::FunctionTraits<const char* (* const&)()>’}
                    122 |     using ReturnType = typename Helpers::FunctionTraits::ReturnType;
                        |           ^~~~~~~~~~
                  In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                                   from ../httptest/main.cpp:2:
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h: In instantiation of ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’:
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
                  ../httptest/main.cpp:16:15:   required from here
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59: error: no matching function for call to ‘QHttpServerRouter::bindCaptured(const char* (* const&)(), const QRegularExpressionMatch&)’
                    135 |             auto boundViewHandler = router()->bindCaptured(viewHandler, match);
                        |                                     ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
                  In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:9,
                                   from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                                   from ../httptest/main.cpp:2:
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39: note: candidate: ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const’
                     57 |     typename ViewTraits::BindableType bindCaptured(ViewHandler &&handler,
                        |                                       ^~~~~~~~~~~~
                  /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39: note:   substitution of deduced template arguments resulted in errors seen above
                  make: *** [Makefile:834: main.o] Error 1
                  14:38:56: The process "/usr/bin/make" exited with code 2.
                  Error while building/deploying project httptest (kit: Desktop Qt 6.4.3 GCC 64bit)
                  When executing step "Make"
                  14:38:56: Elapsed time: 00:01.
                  
                  J 1 Reply Last reply 30 Mar 2023, 12:54
                  0
                  • T Tarae
                    30 Mar 2023, 12:52

                    @jsulm Ok, here it is:

                    14:38:56: Running steps for project httptest...
                    14:38:56: Configuration unchanged, skipping qmake step.
                    14:38:56: Starting: "/usr/bin/make" -j8
                    /home/user/Qt/6.4.3/gcc_64/bin/qmake -o Makefile ../httptest/httptest.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug
                    g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -fPIC -D_REENTRANT -DQT_QML_DEBUG -DQT_HTTPSERVER_LIB -DQT_WEBSOCKETS_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../httptest -I. -I/home/user/Qt/6.4.3/gcc_64/include -I/home/user/Qt/6.4.3/gcc_64/include/QtHttpServer -I/home/user/Qt/6.4.3/gcc_64/include/QtWebSockets -I/home/user/Qt/6.4.3/gcc_64/include/QtNetwork -I/home/user/Qt/6.4.3/gcc_64/include/QtCore -I. -I/home/user/Qt/6.4.3/gcc_64/mkspecs/linux-g++ -o main.o ../httptest/main.cpp
                    In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:8,
                                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:8,
                                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:9,
                                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                                     from ../httptest/main.cpp:2:
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h: In instantiation of ‘struct QtPrivate::FunctionTraits<const char* (* const&)()>’:
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h:65:11:   required from ‘struct QtPrivate::ViewTraits<const char* (* const&)(), false>’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:18:8:   required from ‘struct QtPrivate::RouterViewTraitsHelper<const char* (* const&)(), false>’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:122:11:   required from ‘struct QHttpServerRouterViewTraits<const char* (* const&)(), false>’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39:   required by substitution of ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const [with ViewHandler = const char* (* const&)(); ViewTraits = QHttpServerRouterViewTraits<const char* (* const&)(), false>]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59:   required from ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
                    ../httptest/main.cpp:16:15:   required from here
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h:42:56: error: ‘operator()’ is not a member of ‘std::remove_cv<const char* (* const)()>::type’ {aka ‘const char* (*)()’}
                       42 | struct FunctionTraits : public FunctionTraits<decltype(&q20::remove_cvref_t<T>::operator())>{};
                          |                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h: In instantiation of ‘struct QtPrivate::ViewTraits<const char* (* const&)(), false>’:
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:18:8:   required from ‘struct QtPrivate::RouterViewTraitsHelper<const char* (* const&)(), false>’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:122:11:   required from ‘struct QHttpServerRouterViewTraits<const char* (* const&)(), false>’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39:   required by substitution of ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const [with ViewHandler = const char* (* const&)(); ViewTraits = QHttpServerRouterViewTraits<const char* (* const&)(), false>]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59:   required from ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
                    ../httptest/main.cpp:16:15:   required from here
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h:65:11: error: ‘ArgumentCount’ is not a member of ‘QtPrivate::FunctionTraits<const char* (* const&)()>’
                       65 |     using ArgumentIndexes = typename Indexes<FTraits::ArgumentCount>::Value;
                          |           ^~~~~~~~~~~~~~~
                    In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:8,
                                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:9,
                                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                                     from ../httptest/main.cpp:2:
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h: In instantiation of ‘struct QHttpServerRouterViewTraits<const char* (* const&)(), false>’:
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39:   required by substitution of ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const [with ViewHandler = const char* (* const&)(); ViewTraits = QHttpServerRouterViewTraits<const char* (* const&)(), false>]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59:   required from ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
                    ../httptest/main.cpp:16:15:   required from here
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:122:11: error: no type named ‘ReturnType’ in ‘using FunctionTraits = using FTraits = struct QtPrivate::FunctionTraits<const char* (* const&)()>’ {aka ‘struct QtPrivate::FunctionTraits<const char* (* const&)()>’}
                      122 |     using ReturnType = typename Helpers::FunctionTraits::ReturnType;
                          |           ^~~~~~~~~~
                    In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                                     from ../httptest/main.cpp:2:
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h: In instantiation of ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’:
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
                    ../httptest/main.cpp:16:15:   required from here
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59: error: no matching function for call to ‘QHttpServerRouter::bindCaptured(const char* (* const&)(), const QRegularExpressionMatch&)’
                      135 |             auto boundViewHandler = router()->bindCaptured(viewHandler, match);
                          |                                     ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
                    In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:9,
                                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                                     from ../httptest/main.cpp:2:
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39: note: candidate: ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const’
                       57 |     typename ViewTraits::BindableType bindCaptured(ViewHandler &&handler,
                          |                                       ^~~~~~~~~~~~
                    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39: note:   substitution of deduced template arguments resulted in errors seen above
                    make: *** [Makefile:834: main.o] Error 1
                    14:38:56: The process "/usr/bin/make" exited with code 2.
                    Error while building/deploying project httptest (kit: Desktop Qt 6.4.3 GCC 64bit)
                    When executing step "Make"
                    14:38:56: Elapsed time: 00:01.
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 30 Mar 2023, 12:54 last edited by
                    #8

                    @Tarae
                    Yep, that's pretty complex!

                    Wait for @jsulm's better reply, but maybe you are required to put in the two optional parameters for this free/C function to be acceptable? Worth a try?

                    T 1 Reply Last reply 30 Mar 2023, 12:56
                    0
                    • J JonB
                      30 Mar 2023, 12:54

                      @Tarae
                      Yep, that's pretty complex!

                      Wait for @jsulm's better reply, but maybe you are required to put in the two optional parameters for this free/C function to be acceptable? Worth a try?

                      T Offline
                      T Offline
                      Tarae
                      wrote on 30 Mar 2023, 12:56 last edited by
                      #9

                      @JonB As you suggested, I've tried this:

                      const char * responseFunction(const QHttpServerRequest & request, QHttpServerResponder && responder)
                      {
                        return "Hello world!";
                      }
                      

                      Still doesn't compile, same error.

                      J J 2 Replies Last reply 30 Mar 2023, 12:58
                      0
                      • T Tarae
                        30 Mar 2023, 12:56

                        @JonB As you suggested, I've tried this:

                        const char * responseFunction(const QHttpServerRequest & request, QHttpServerResponder && responder)
                        {
                          return "Hello world!";
                        }
                        

                        Still doesn't compile, same error.

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 30 Mar 2023, 12:58 last edited by
                        #10

                        @Tarae said in QHttpServer: use function pointer as ViewHandler:

                        const char * responseFunction

                        Try without const:

                        char * responseFunction(...
                        

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

                        T 1 Reply Last reply 30 Mar 2023, 13:01
                        0
                        • J jsulm
                          30 Mar 2023, 12:58

                          @Tarae said in QHttpServer: use function pointer as ViewHandler:

                          const char * responseFunction

                          Try without const:

                          char * responseFunction(...
                          
                          T Offline
                          T Offline
                          Tarae
                          wrote on 30 Mar 2023, 13:01 last edited by
                          #11

                          @jsulm No, const char *, char *, QString, const QString, QByteArray, const QByteArray. Still same error.

                          1 Reply Last reply
                          0
                          • T Tarae
                            30 Mar 2023, 12:56

                            @JonB As you suggested, I've tried this:

                            const char * responseFunction(const QHttpServerRequest & request, QHttpServerResponder && responder)
                            {
                              return "Hello world!";
                            }
                            

                            Still doesn't compile, same error.

                            J Offline
                            J Offline
                            JonB
                            wrote on 30 Mar 2023, 13:01 last edited by JonB
                            #12

                            @Tarae
                            I'm only guessing, but since those parameters are optional try specifying defaults for them. Assuming you can do that in a free/C function, I don't know.

                            The const char * for the return value is correct.

                            BTW: if all else fails, you can always make it a lambda like the examples show and have that lambda simply call your free function....

                            T 1 Reply Last reply 30 Mar 2023, 13:23
                            0
                            • J JonB
                              30 Mar 2023, 13:01

                              @Tarae
                              I'm only guessing, but since those parameters are optional try specifying defaults for them. Assuming you can do that in a free/C function, I don't know.

                              The const char * for the return value is correct.

                              BTW: if all else fails, you can always make it a lambda like the examples show and have that lambda simply call your free function....

                              T Offline
                              T Offline
                              Tarae
                              wrote on 30 Mar 2023, 13:23 last edited by
                              #13

                              @JonB specifying defaults for const QHttpServerRequest & and QHttpServerResponder && is not that easy. But I don't feel like it will solve my problem.
                              I know I can use a lambda to call my function, but the documentation says function pointer, so I want to figure it out.

                              C 1 Reply Last reply 31 Mar 2023, 08:04
                              0
                              • T Tarae
                                30 Mar 2023, 13:23

                                @JonB specifying defaults for const QHttpServerRequest & and QHttpServerResponder && is not that easy. But I don't feel like it will solve my problem.
                                I know I can use a lambda to call my function, but the documentation says function pointer, so I want to figure it out.

                                C Online
                                C Online
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 31 Mar 2023, 08:04 last edited by
                                #14

                                From my pov a plain function is currently not possible so either the documentation is wrong or the implementation. Since there is an easy workaround for it I would guess the solution is to adjust the documentation. Please create a bug report for it and post the bug number here.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                T 1 Reply Last reply 31 Mar 2023, 13:47
                                2
                                • J Offline
                                  J Offline
                                  JonB
                                  wrote on 31 Mar 2023, 08:11 last edited by JonB
                                  #15

                                  If this is what @Christian-Ehrlicher says, no wonder we could not figure it yesterday! :)

                                  C 1 Reply Last reply 31 Mar 2023, 08:15
                                  0
                                  • J JonB
                                    31 Mar 2023, 08:11

                                    If this is what @Christian-Ehrlicher says, no wonder we could not figure it yesterday! :)

                                    C Online
                                    C Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 31 Mar 2023, 08:15 last edited by
                                    #16

                                    A plain function does not work but a std::function<> since the template magic expects a callable:

                                    std::function<const char* ()>  resp = &responseFunction;
                                    server.route("/", resp);
                                    

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    J 1 Reply Last reply 31 Mar 2023, 08:39
                                    2
                                    • C Christian Ehrlicher
                                      31 Mar 2023, 08:15

                                      A plain function does not work but a std::function<> since the template magic expects a callable:

                                      std::function<const char* ()>  resp = &responseFunction;
                                      server.route("/", resp);
                                      
                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 31 Mar 2023, 08:39 last edited by JonB
                                      #17

                                      @Christian-Ehrlicher
                                      I am whistling in the dark here, but I'm a student of C++ so I am interested. I am not as expert as you. The issue is "a callable". I do not have Qt6 so cannot test QHttpServer::route() If you don't want to use a lambda or std::function here, could you use something like:

                                      struct callable
                                      {
                                        const char *operator()() const
                                        {
                                          std::cout << "Hello World from a callable!\n";
                                          return "Hello World!";
                                        }
                                      };
                                      
                                      const callable responseFunction;
                                      server.route("/", &responseFunction);
                                      

                                      Maybe this is the sort of thing a lambda or std::function() does which is different from a plain function?

                                      1 Reply Last reply
                                      0
                                      • C Christian Ehrlicher
                                        31 Mar 2023, 08:04

                                        From my pov a plain function is currently not possible so either the documentation is wrong or the implementation. Since there is an easy workaround for it I would guess the solution is to adjust the documentation. Please create a bug report for it and post the bug number here.

                                        T Offline
                                        T Offline
                                        Tarae
                                        wrote on 31 Mar 2023, 13:47 last edited by
                                        #18

                                        @Christian-Ehrlicher I've created a bug report: https://bugreports.qt.io/browse/QTBUG-112484

                                        C 1 Reply Last reply 25 Apr 2023, 18:34
                                        2
                                        • T Tarae
                                          31 Mar 2023, 13:47

                                          @Christian-Ehrlicher I've created a bug report: https://bugreports.qt.io/browse/QTBUG-112484

                                          C Online
                                          C Online
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on 25 Apr 2023, 18:34 last edited by
                                          #19

                                          And fixed

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          1 Reply Last reply
                                          0
                                          • T Tarae has marked this topic as solved on 19 Jun 2023, 16:32

                                          • Login

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