compatible_smart_ptr.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ++++++++++++++++++++++++++++++++++
  2. |Boost| Pointer Container Library
  3. ++++++++++++++++++++++++++++++++++
  4. .. |Boost| image:: boost.png
  5. Compatible Smart Pointer Type
  6. -----------------------------
  7. When specifying parameter or return types in interfaces, the documentation
  8. for this library uses the pseudo-type
  9. .. parsed-literal::
  10. *compatible-smart-ptr*\ <T>
  11. to indicate that the compiler C++ standard is being used to
  12. selectively provide or remove interfaces with ``std::auto_ptr<T>`` or
  13. ``std::unique_ptr<T>``. The exact meaning varies depending on whether
  14. the smart pointer type is a parameter or a return type.
  15. **Parameter Types:**
  16. An interface such as
  17. .. parsed-literal::
  18. void container::push_back( *compatible-smart-ptr*\ <T> );
  19. indicates that an overload of ``container::push_back`` is present for
  20. one or both of ``std::auto_ptr<T>``, ``std::unique_ptr<T>``:
  21. Boost.Pointer Container provides an overload for each type supported
  22. by the compiler. To be completely explicit, if the compiler provides
  23. ``std::auto_ptr``, then
  24. .. parsed-literal::
  25. void container::push_back( std::auto_ptr<T> );
  26. is present. If the compiler provides ``std::unique_ptr``, then
  27. .. parsed-literal::
  28. void container::push_back( std::unique_ptr<T> );
  29. is present. And if the compiler provides both, both overloads are
  30. present.
  31. In practice this means that C++98/03 users have access to
  32. ``std::auto_ptr`` overloads, C++11/14 users have access to
  33. overloads taking both ``std::auto_ptr`` and ``std::unique_ptr``, and
  34. users of C++17 and onwards only have access to ``std::unique_ptr``
  35. overloads.
  36. The convention outlined above implies that in certain cases the
  37. documentation will make reference to a single function taking the
  38. compatible smart pointer pseudo parameter, when in fact two distinct
  39. overloaded functions are present. Of course the actual interface
  40. depends on compiler settings, so for clarity the `class hierarchy
  41. reference <reversible_ptr_container.html>`_ will only ever refer to a
  42. single function.
  43. **Return Types:**
  44. The case of return types is handled along the same lines as parameter
  45. types, subject of course to the restriction that C++ functions cannot
  46. be overloaded by return type. Thus, an interface such as
  47. .. parsed-literal::
  48. *compatible-smart-ptr*\ <T> container::release( );
  49. means that precisely one of ``std::auto_ptr<T>`` or
  50. ``std::unique_ptr<T>`` is used as the return type. If the compiler
  51. provides ``std::auto_ptr<T>``, then
  52. .. parsed-literal::
  53. std::auto_ptr<T> container::release( );
  54. is present, even if the compiler provides ``std::unique_ptr``. For
  55. compilers that only provide ``std::unique_ptr``, the interface above
  56. becomes
  57. .. parsed-literal::
  58. std::unique_ptr<T> container::release( );
  59. In practice, this means that for users of C++98/03/11/14, such return
  60. types are always ``std::auto_ptr``; for users of C++17 and onwards the
  61. return type is ``std::unique_ptr``.
  62. **Details:**
  63. The ISO C++11 standard saw the addition of the smart pointer class template
  64. ``std::unique_ptr``, and with it the formal deprecation of
  65. ``std::auto_ptr``. After spending C++11 and C++14 with deprecated
  66. status, ``std::auto_ptr`` has been formally removed as of
  67. the ISO C++17 standard. As such, headers mentioning
  68. ``std::auto_ptr`` may be unusable in standard library
  69. implementations which disable ``std::auto_ptr`` when C++17 or later
  70. is used. Boost.Pointer Container predates the existence of
  71. ``std::unique_ptr``, and since Boost v. ``1.34`` it has provided
  72. ``std::auto_ptr`` overloads for its interfaces. To provide
  73. compatibility across a range of C++ standards, macros are used for
  74. compile-time overloading or replacement of ``std::auto_ptr`` interfaces with
  75. ``std::unique_ptr`` interfaces.
  76. `Boost.Config <../../config/index.html>`_ defines the macro
  77. ``BOOST_NO_CXX11_SMART_PTR`` for compilers where
  78. ``std::unique_ptr`` is not available, and ``BOOST_NO_AUTO_PTR`` for
  79. compilers where ``std::auto_ptr`` is removed (or is defective). These
  80. macros are used for compile-time selection of interfaces depending on
  81. parameter and return type. For interfaces that take smart pointer
  82. parameters, Boost.Pointer Container uses ``BOOST_NO_AUTO_PTR`` and
  83. ``BOOST_NO_CXX11_SMART_PTR`` independently of each other to provide
  84. interfaces taking one or both of ``std::auto_ptr``,
  85. ``std::unique_ptr`` as parameters. For interfaces with smart pointer
  86. return types, the Boost.Config macros are used first to check if
  87. ``std::auto_ptr`` is available, providing ``std::unique_ptr`` as the
  88. return type only for compilers that provide ``std::unique_ptr`` but
  89. not ``std::auto_ptr``.
  90. Thus, all mentions of
  91. .. parsed-literal::
  92. *compatible-smart-ptr*\ <T>
  93. shall be understood to mean that `Boost.Config
  94. <../../config/index.html>`_ has been used as outlined above to provide
  95. a smart pointer interface that is compatible with compiler settings.
  96. **Navigate**
  97. - `home <ptr_container.html>`_
  98. - `reference <reference.html>`_
  99. .. raw:: html
  100. <hr>
  101. :Copyright: Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt__).
  102. __ http://www.boost.org/LICENSE_1_0.txt