7

Reading this, I got the impression that this code should work:

class Connection : public std::enable_shared_from_this<Connection>
{
public:
    Connection(tcp::socket&& socket) : socket_(std::move(socket)) {}
private:
    tcp::socket socket_;
};

But the compiler issues this error in the constructor:

Call to implicitly-deleted copy constructor of 'tcp::socket' (aka'basic_stream_socket<boost::asio::ip::tcp>')

I have also defined BOOST_ASIO_HAS_MOVE . I use Xcode 4.6.3 and in the compiler settings I have this defined:

C++ Language dialect: GNU++11[-std=gnu++11]
C++ Standard Library: libc++(LLVM C++ standard library with C++11 support)
Adrian
  • 19,440
  • 34
  • 112
  • 219
  • 2
    Could you show the code that calls `Connecion` constructor? – Igor R. Jul 28 '13 at 16:23
  • @Igor R: I have another server class which I use it in main(), but the Connection class is not used anywhere. It is just declared – Adrian Jul 28 '13 at 16:27
  • 1
    So you are getting an error about calling an implicitly-deleted copy constructor, but no code constructs a `Connection` object? Does the compiler provide a trace of where the error was instantiated? This problem normal manifest itself when the code calling the constructor passes socket as an rvalue reference, rather than converting it to an xvalue via `std::move`. – Tanner Sansbury Jul 29 '13 at 13:51
  • This is the highlighted part: std::move(socket). From what I see, the problem is that tcp::socket has a move constructor defined, but no copy constructor so, the implicit copy constructor gets deleted. – Adrian Jul 29 '13 at 16:16
  • 1
    please edit your question to include a minimally complete example demonstrating the problem. [I cannot reproduce](http://coliru.stacked-crooked.com/view?id=bf0c71becd40e83fe409a710475ff4c7-dd943382a4c9c05df1e1b0f5fb4ff1a2). – Sam Miller Jul 29 '13 at 20:51
  • @SamMiller: I get the same error with your example. Plus I also get error in main when declaring the shared_ptr saying: No matching constructor for initialisation of const std::shared_ptr. So we can use that example. I see that your compiling with g++. Can't imagine why for me it doesn't compile correctly. I use Clang – Adrian Jul 30 '13 at 06:52
  • I managed to fix my issues, by using a io_service object in my Connection constructor instead of tcp::socket. Couldn't find any other way to fix this. – Adrian Jul 30 '13 at 12:49
  • I used clang on my mac too, with boost 1.54, the same code compiles fine: `clang++ -stdlib=libc++ --std=gnu++11 -isystem /usr/local/include -L /usr/local/lib -lboost_system-mt move.cc`. Please include the exact code you're compiling in your question. – Sam Miller Jul 30 '13 at 16:37

1 Answers1

5

You need to have BOOST_ASIO_HAS_MOVE defined before including the ASIO headers. If you don't, move support is disabled. See asio/basic_stream_socket.hpp.

https://svn.boost.org/trac/boost/ticket/8959

sdkljhdf hda
  • 1,387
  • 9
  • 17
  • Explicitly defining BOOST_ASIO_HAS_MOVE didn't work for me. I had to modify `/usr/include/boost/asio/detail/config.hpp`. I've added `// Clang / libc++ detection` and replaced `// Support move construction and assignment on compilers known to allow it.` sections with the ones from: http://www.boost.org/doc/libs/1_55_0/boost/asio/detail/config.hpp – Alex Bitek Jul 08 '14 at 11:48