error: no matching function for call to 'uWS::TemplatedApp<false>::ws<main()::UserData>(const char [3], <brace-enclosed initializer list>)'

error: no matching function for call to 'uWS::TemplatedApp<false>::ws<main()::UserData>(const char [3], <brace-enclosed initializer list>)'

我正在使用 uwebsockets 创建 ws 服务器。

main.cpp:

int main()
{
  struct UserData {

  };

  uWS::App().ws<UserData>("/*", {

      /* Just a few of the available handlers */
      .open = [](auto *ws) {
          /* MQTT syntax */
          ws->subscribe("sensors/+/house");
      },
      .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
          ws->send(message, opCode);
      }

  }).listen(9001, [](auto *listenSocket) {

      if (listenSocket) {
          std::cout << "Listening on port " << 9001 << std::endl;
      }

  }).run();
  return 0;
}

构建时的错误信息:

……main.cpp:22:4: note:   cannot convert '{<lambda closure object>main()::<lambda(auto:11*)>{}, <lambda closure object>main()::<lambda(auto:12*, std::string_view, uWS::OpCode)>{}}' (type '<brace-enclosed initializer list>') to type 'uWS::TemplatedApp<false>::WebSocketBehavior<main()::UserData>&&'
   }).listen(9001, [](auto *listenSocket) {
    ^

环境:

OS: Windows10 64 位

IDE: QtCreator

编译器:MinGW 8.1.0 32 位

c++标准:17

uwebsockets: 19.2.0

因为编译器无法将'brace-enclosed initializer list'转换为T&&。可以通过声明结构并将其转换为右值引用来解决。

  struct UserData {

  };

  uWS::TemplatedApp<false>::WebSocketBehavior<UserData> wsb = {
    /* Just a few of the available handlers */
    .open = [](auto *ws) {
      /* MQTT syntax */
      ws->subscribe("sensors/+/house");
    },
    .message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
      ws->send(message, opCode);
    }
  };

  uWS::App().ws<UserData>("/*", std::move(wsb)).listen(9001, [](auto *listenSocket) {
      if (listenSocket) {
          std::cout << "Listening on port " << 9001 << std::endl;
      }
  }).run();