C++:MSVC 仅使用 Boost::asio 检测到语法错误

C++: syntax error detected by MSVC only using Boost::asio

我正在使用 Boost::ASIO.

在 C++ 中制作基本的客户端-服务器架构

MSVC 在此行报告 syntax error: '.' (C2059)

void
Server::start()
{
  m_stopped = false;
  listen_one();

  m_runner = std::make_unique<std::thread>([this](){
    io_service.run(); // <- syntax error: '.'
  });

  m_runner->detach();
}
void
Client::init()
{
  m_socket->connect(boost::asio::ip::tcp::endpoint(
                              boost::asio::ip::address::from_string(m_ip),
                              static_cast<short unsigned int>(m_port)));
  
  Connection::init();

  m_runner = std::make_unique<std::thread>([this](){
    io_service.run(); // <- this line is fine
  });

  m_runner->detach();
}

基本调试:


为什么 MSVC 检测到语法错误,而 GCC 和 Clang 都检测不到?

Can you try this->io_service.run()?

MSVC 长期以来一直受到两阶段查找的挑战。

也许就是这样。 this-> 明确表示 io_service 代表 class 成员。在显示的 lambda 中,它通常不重要(除非 class 实际上被声明为模板的一部分?)。

另一个起作用的因素可能是名称隐藏:io_service 隐藏了类型的名称。如果该类型名称在没有命名空间限定的情况下可见,则可能会导致 MSVC 混淆。