1

I'm making a basic client-server architecture in C++ using Boost::ASIO.

MSVC reports a syntax error: '.' (C2059) at this line:

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

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

  m_runner->detach();
}
  • GCC and Clang both compile the same code on Linux and macOS just fine.
  • The code has been changed several times and compiled several imes, and the error persists.
  • There is this other block of code, which MSVC does not detect as an error:
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();
}

Basic debugging:

  • io_service, m_stopped, m_runner are member variables of Server, and listen_one() is a member function.
  • Server does not inherit anything. (If relevant, Client inherits another class, Connection)

Why does MSVC detect a syntax error, but neither GCC nor Clang do?

Sampi
  • 27
  • 3
  • Can you try `this->io_service.run()`? MSVC has been challenged with two-phase lookup for a very long time. Perhaps that's it – sehe Dec 14 '20 at 21:46
  • @sehe Well, somehow, it solved the problem. Please post your comment as an answer so I can accept it :) – Sampi Dec 15 '20 at 02:42

1 Answers1

1

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

MSVC has been challenged with two-phase lookup for a very long time.

Perhaps that's it. What this-> makes explicit is that io_service represents a class member. In the lambda shown it shouldn't usually matter (except perhaps if the class is actually declared [as part of] a template?).

Another factor at play could be name shadowing: io_service shadows the name of the type. If that type name is visible without namespace qualification, then it might cause confusion to MSVC.

sehe
  • 374,641
  • 47
  • 450
  • 633