I have followed this example to set up an asynchronous UDP receive. But it fails on compilation:
g++ -Wall -Wconversion -Wfatal-errors -Wextra -std=c++11 test1.cpp
Error message:
In file included from /usr/include/c++/5/ext/alloc_traits.h:36:0,
from /usr/include/c++/5/bits/basic_string.h:40,
from /usr/include/c++/5/string:52,
from /usr/include/c++/5/stdexcept:39,
from /usr/include/c++/5/array:38,
from /usr/include/c++/5/tuple:39,
from /usr/include/c++/5/functional:55,
from /usr/include/c++/5/thread:39,
from test1.cpp:1:
/usr/include/c++/5/bits/alloc_traits.h: In instantiation of ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::promise<long unsigned int>; _Args = {const std::allocator_arg_t&, std::allocator<void>}; _Tp = void; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<void>]’:
/usr/include/c++/5/bits/shared_ptr_base.h:522:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {const std::allocator_arg_t&, std::allocator<void>}; _Tp = std::promise<long unsigned int>; _Alloc = std::allocator<void>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/include/c++/5/bits/shared_ptr_base.h:617:4: required from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = std::promise<long unsigned int>; _Alloc = std::allocator<void>; _Args = {const std::allocator_arg_t&, std::allocator<void>}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/include/c++/5/bits/shared_ptr_base.h:1097:35: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<void>; _Args = {const std::allocator_arg_t&, std::allocator<void>}; _Tp = std::promise<long unsigned int>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/include/c++/5/bits/shared_ptr.h:319:64: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<void>; _Args = {const std::allocator_arg_t&, std::allocator<void>}; _Tp = std::promise<long unsigned int>]’
/usr/include/c++/5/bits/shared_ptr.h:620:39: required from ‘std::shared_ptr<_Tp1> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = std::promise<long unsigned int>; _Alloc = std::allocator<void>; _Args = {const std::allocator_arg_t&, std::allocator<void>}]’
/usr/include/boost/asio/impl/use_future.hpp:39:56: required from ‘boost::asio::detail::promise_handler<T>::promise_handler(boost::asio::use_future_t<OtherAllocator>) [with Allocator = std::allocator<void>; T = long unsigned int]’
/usr/include/boost/asio/async_result.hpp:62:21: required from ‘boost::asio::detail::async_result_init<Handler, Signature>::async_result_init(Handler&&) [with Handler = const boost::asio::use_future_t<>&; Signature = void(boost::system::error_code, long unsigned int)]’
/usr/include/boost/asio/datagram_socket_service.hpp:410:51: required from ‘typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::datagram_socket_service<Protocol>::async_receive_from(boost::asio::datagram_socket_service<Protocol>::implementation_type&, const MutableBufferSequence&, boost::asio::datagram_socket_service<Protocol>::endpoint_type&, boost::asio::socket_base::message_flags, ReadHandler&&) [with MutableBufferSequence = boost::asio::mutable_buffers_1; ReadHandler = const boost::asio::use_future_t<>&; Protocol = boost::asio::ip::udp; typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type = std::future<long unsigned int>; boost::asio::datagram_socket_service<Protocol>::implementation_type = boost::asio::detail::reactive_socket_service<boost::asio::ip::udp>::implementation_type; boost::asio::datagram_socket_service<Protocol>::endpoint_type = boost::asio::ip::basic_endpoint<boost::asio::ip::udp>; boost::asio::socket_base::message_flags = int]’
/usr/include/boost/asio/basic_datagram_socket.hpp:897:51: required from ‘typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::async_receive_from(const MutableBufferSequence&, boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::endpoint_type&, ReadHandler&&) [with MutableBufferSequence = boost::asio::mutable_buffers_1; ReadHandler = const boost::asio::use_future_t<>&; Protocol = boost::asio::ip::udp; DatagramSocketService = boost::asio::datagram_socket_service<boost::asio::ip::udp>; typename boost::asio::async_result<typename boost::asio::handler_type<WriteHandler, void(boost::system::error_code, long unsigned int)>::type>::type = std::future<long unsigned int>; boost::asio::basic_datagram_socket<Protocol, DatagramSocketService>::endpoint_type = boost::asio::ip::basic_endpoint<boost::asio::ip::udp>]’
test1.cpp:25:28: required from here
/usr/include/c++/5/bits/alloc_traits.h:530:4: error: ‘using allocator_type = class std::allocator<void> {aka class std::allocator<void>}’ has no member named ‘construct’
{ __a.construct(__p, std::forward<_Args>(__args)...); }
^
Code:
#include <thread>
#include <iostream>
#include <future>
#include <boost/array.hpp>
#include <boost/asio/use_future.hpp>
#include <boost/asio.hpp>
int main()
{
boost::asio::io_service ioserv;
boost::asio::ip::udp::endpoint ep(boost::asio::ip::udp::v4(),(ushort)1414);
boost::asio::ip::udp::socket mysocket(ioserv,ep);
boost::asio::socket_base::reuse_address option(true);
mysocket.set_option(option);
boost::asio::ip::udp::endpoint sender_ep;
while(1)
{
std::array<char, 128> recv_buf;
boost::asio::ip::udp::endpoint sender_endpoint;
std::future<std::size_t> recv_length =
mysocket.async_receive_from(
boost::asio::buffer(recv_buf),
sender_endpoint,
boost::asio::use_future); // <<<< line 25: error
// Do other things here while the receive completes.
std::cout.write(
recv_buf.data(),
recv_length.get()); // Blocks until receive is complete.
}
return 0;
}
Looks like this bug was fixed in:
Which looks like it made it into boost 1.60