cpp2011.qbk 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:cpp2011 C++ 2011 Support]
  8. [link boost_asio.overview.cpp2011.move_objects Movable I/O Objects]
  9. [link boost_asio.overview.cpp2011.move_handlers Movable Handlers]
  10. [link boost_asio.overview.cpp2011.variadic Variadic Templates]
  11. [link boost_asio.overview.cpp2011.array Array Container]
  12. [link boost_asio.overview.cpp2011.atomic Atomics]
  13. [link boost_asio.overview.cpp2011.shared_ptr Shared Pointers]
  14. [link boost_asio.overview.cpp2011.chrono Chrono]
  15. [link boost_asio.overview.cpp2011.futures Futures]
  16. [section:move_objects Movable I/O Objects]
  17. When move support is available (via rvalue references), Boost.Asio allows move
  18. construction and assignment of sockets, serial ports, POSIX descriptors and
  19. Windows handles.
  20. Move support allows you to write code like:
  21. tcp::socket make_socket(io_context& i)
  22. {
  23. tcp::socket s(i);
  24. ...
  25. std::move(s);
  26. }
  27. or:
  28. class connection : public enable_shared_from_this<connection>
  29. {
  30. private:
  31. tcp::socket socket_;
  32. ...
  33. public:
  34. connection(tcp::socket&& s) : socket_(std::move(s)) {}
  35. ...
  36. };
  37. ...
  38. class server
  39. {
  40. private:
  41. tcp::acceptor acceptor_;
  42. ...
  43. void handle_accept(error_code ec, tcp::socket socket)
  44. {
  45. if (!ec)
  46. std::make_shared<connection>(std::move(socket))->go();
  47. acceptor_.async_accept(...);
  48. }
  49. ...
  50. };
  51. as well as:
  52. std::vector<tcp::socket> sockets;
  53. sockets.push_back(tcp::socket(...));
  54. A word of warning: There is nothing stopping you from moving these objects
  55. while there are pending asynchronous operations, but it is unlikely to be a
  56. good idea to do so. In particular, composed operations like [link
  57. boost_asio.reference.async_read async_read()] store a reference to the stream object.
  58. Moving during the composed operation means that the composed operation may
  59. attempt to access a moved-from object.
  60. Move support is automatically enabled for [^g++] 4.5 and later, when the
  61. [^-std=c++0x] or [^-std=gnu++0x] compiler options are used. It may be disabled
  62. by defining `BOOST_ASIO_DISABLE_MOVE`, or explicitly enabled for other compilers by
  63. defining `BOOST_ASIO_HAS_MOVE`. Note that these macros also affect the availability
  64. of [link boost_asio.overview.cpp2011.move_handlers movable handlers].
  65. [endsect]
  66. [section:move_handlers Movable Handlers]
  67. As an optimisation, user-defined completion handlers may provide move
  68. constructors, and Boost.Asio's implementation will use a handler's move constructor
  69. in preference to its copy constructor. In certain circumstances, Boost.Asio may be
  70. able to eliminate all calls to a handler's copy constructor. However, handler
  71. types are still required to be copy constructible.
  72. When move support is enabled, asynchronous that are documented as follows:
  73. template <typename Handler>
  74. void async_XYZ(..., Handler handler);
  75. are actually declared as:
  76. template <typename Handler>
  77. void async_XYZ(..., Handler&& handler);
  78. The handler argument is perfectly forwarded and the move construction occurs
  79. within the body of `async_XYZ()`. This ensures that all other function
  80. arguments are evaluated prior to the move. This is critical when the other
  81. arguments to `async_XYZ()` are members of the handler. For example:
  82. struct my_operation
  83. {
  84. shared_ptr<tcp::socket> socket;
  85. shared_ptr<vector<char>> buffer;
  86. ...
  87. void operator(error_code ec, size_t length)
  88. {
  89. ...
  90. socket->async_read_some(boost::asio::buffer(*buffer), std::move(*this));
  91. ...
  92. }
  93. };
  94. Move support is automatically enabled for [^g++] 4.5 and later, when the
  95. [^-std=c++0x] or [^-std=gnu++0x] compiler options are used. It may be disabled
  96. by defining `BOOST_ASIO_DISABLE_MOVE`, or explicitly enabled for other compilers by
  97. defining `BOOST_ASIO_HAS_MOVE`. Note that these macros also affect the availability
  98. of [link boost_asio.overview.cpp2011.move_objects movable I/O objects].
  99. [endsect]
  100. [section:variadic Variadic Templates]
  101. When supported by a compiler, Boost.Asio can use variadic templates to implement the
  102. [link boost_asio.reference.basic_socket_streambuf.connect
  103. basic_socket_streambuf::connect()] and [link
  104. boost_asio.reference.basic_socket_iostream.connect basic_socket_iostream::connect()]
  105. functions.
  106. Support for variadic templates is automatically enabled for [^g++] 4.3 and
  107. later, when the [^-std=c++0x] or [^-std=gnu++0x] compiler options are used. It
  108. may be disabled by defining `BOOST_ASIO_DISABLE_VARIADIC_TEMPLATES`, or explicitly
  109. enabled for other compilers by defining `BOOST_ASIO_HAS_VARIADIC_TEMPLATES`.
  110. [endsect]
  111. [section:array Array Container]
  112. Where the standard library provides `std::array<>`, Boost.Asio:
  113. * Provides overloads for the [link boost_asio.reference.buffer buffer()] function.
  114. * Uses it in preference to `boost::array<>` for the
  115. [link boost_asio.reference.ip__address_v4.bytes_type ip::address_v4::bytes_type] and
  116. [link boost_asio.reference.ip__address_v6.bytes_type ip::address_v6::bytes_type]
  117. types.
  118. * Uses it in preference to `boost::array<>` where a fixed size array type is
  119. needed in the implementation.
  120. Support for `std::array<>` is automatically enabled for [^g++] 4.3 and later,
  121. when the [^-std=c++0x] or [^-std=gnu++0x] compiler options are used, as well as
  122. for Microsoft Visual C++ 10. It may be disabled by defining
  123. `BOOST_ASIO_DISABLE_STD_ARRAY`, or explicitly enabled for other compilers by
  124. defining `BOOST_ASIO_HAS_STD_ARRAY`.
  125. [endsect]
  126. [section:atomic Atomics]
  127. Boost.Asio's implementation can use `std::atomic<>` in preference to
  128. `boost::detail::atomic_count`.
  129. Support for the standard atomic integer template is automatically enabled for
  130. [^g++] 4.5 and later, when the [^-std=c++0x] or [^-std=gnu++0x] compiler
  131. options are used. It may be disabled by defining `BOOST_ASIO_DISABLE_STD_ATOMIC`, or
  132. explicitly enabled for other compilers by defining `BOOST_ASIO_HAS_STD_ATOMIC`.
  133. [endsect]
  134. [section:shared_ptr Shared Pointers]
  135. Boost.Asio's implementation can use `std::shared_ptr<>` and `std::weak_ptr<>` in
  136. preference to the Boost equivalents.
  137. Support for the standard smart pointers is automatically enabled for [^g++] 4.3
  138. and later, when the [^-std=c++0x] or [^-std=gnu++0x] compiler options are used,
  139. as well as for Microsoft Visual C++ 10. It may be disabled by defining
  140. `BOOST_ASIO_DISABLE_STD_SHARED_PTR`, or explicitly enabled for other compilers by
  141. defining `BOOST_ASIO_HAS_STD_SHARED_PTR`.
  142. [endsect]
  143. [section:chrono Chrono]
  144. Boost.Asio provides timers based on the `std::chrono` facilities via the [link
  145. boost_asio.reference.basic_waitable_timer basic_waitable_timer] class template.
  146. The typedefs [link boost_asio.reference.system_timer system_timer], [link
  147. boost_asio.reference.steady_timer steady_timer] and [link
  148. boost_asio.reference.high_resolution_timer high_resolution_timer] utilise the
  149. standard clocks `system_clock`, `steady_clock` and `high_resolution_clock`
  150. respectively.
  151. Support for the `std::chrono` facilities is automatically enabled for [^g++]
  152. 4.6 and later, when the [^-std=c++0x] or [^-std=gnu++0x] compiler options are
  153. used. (Note that, for [^g++], the draft-standard `monotonic_clock` is used in
  154. place of `steady_clock`.) Support may be disabled by defining
  155. `BOOST_ASIO_DISABLE_STD_CHRONO`, or explicitly enabled for other compilers by
  156. defining `BOOST_ASIO_HAS_STD_CHRONO`.
  157. When standard `chrono` is unavailable, Boost.Asio will otherwise use the Boost.Chrono
  158. library. The [link boost_asio.reference.basic_waitable_timer basic_waitable_timer]
  159. class template may be used with either.
  160. [endsect]
  161. [section:futures Futures]
  162. The `boost::asio::use_future` special value provides first-class support for returning a
  163. C++11 `std::future` from an asynchronous operation's initiating function.
  164. To use `boost::asio::use_future`, pass it to an asynchronous operation instead of
  165. a normal completion handler. For example:
  166. std::future<std::size_t> length =
  167. my_socket.async_read_some(my_buffer, boost::asio::use_future);
  168. Where a handler signature has the form:
  169. void handler(boost::system::error_code ec, result_type result);
  170. the initiating function returns a `std::future` templated on `result_type`.
  171. In the above example, this is `std::size_t`. If the asynchronous operation
  172. fails, the `error_code` is converted into a `system_error` exception and
  173. passed back to the caller through the future.
  174. Where a handler signature has the form:
  175. void handler(boost::system::error_code ec);
  176. the initiating function returns `std::future<void>`. As above, an error
  177. is passed back in the future as a `system_error` exception.
  178. [link boost_asio.reference.use_future use_future],
  179. [link boost_asio.reference.use_future_t use_future_t],
  180. [link boost_asio.examples.cpp11_examples.futures Futures example (C++11)].
  181. [endsect]
  182. [endsect]