90_dependencies.qbk 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. [/
  2. Boost.Optional
  3. Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ]
  8. [section Dependencies and Portability]
  9. [section Dependencies]
  10. The implementation uses the following other Boost modules:
  11. # assert
  12. # config
  13. # core
  14. # detail
  15. # move
  16. # mpl
  17. # static_assert
  18. # throw_exception
  19. # type_traits
  20. # utility
  21. [endsect]
  22. [section Emplace operations in older compilers][#optional_emplace_workaround]
  23. Certain constructors and functions in the interface of `optional` perform a 'perfect forwarding' of arguments:
  24. template<class... Args> optional(in_place_init_t, Args&&... args);
  25. template<class... Args> optional(in_place_init_if_t, bool condition, Args&&... args);
  26. template<class... Args> void emplace(Args&&... args);
  27. On compilers that do not support variadic templates, each of these functions is substituted with two overloads, one forwarding a single argument, the other forwarding zero arguments. This forms the following set:
  28. template<class Arg> optional(in_place_init_t, Arg&& arg);
  29. optional(in_place_init_t);
  30. template<class Arg> optional(in_place_init_if_t, bool condition, Arg&& arg);
  31. optional(in_place_init_if_t, bool condition);
  32. template<class Arg> void emplace(Arg&& arg);
  33. void emplace();
  34. On compilers that do not support rvalue references, each of these functions is substituted with three overloadss: taking `const` and non-`const` lvalue reference, and third forwarding zero arguments. This forms the following set:
  35. template<class Arg> optional(in_place_init_t, const Arg& arg);
  36. template<class Arg> optional(in_place_init_t, Arg& arg);
  37. optional(in_place_init_t);
  38. template<class Arg> optional(in_place_init_if_t, bool condition, const Arg& arg);
  39. template<class Arg> optional(in_place_init_if_t, bool condition, Arg& arg);
  40. optional(in_place_init_if_t, bool condition);
  41. template<class Arg> void emplace(const Arg& arg);
  42. template<class Arg> void emplace(Arg& arg);
  43. void emplace();
  44. This workaround addressess about 40% of all use cases. If this is insufficient, you need to resort to using [link boost_optional.tutorial.in_place_factories In-Place Factories].
  45. [endsect]
  46. [section Optional Reference Binding][#optional_reference_binding]
  47. A number of compilers incorrectly treat const lvalues of integral type as rvalues, and create an illegal temporary when binding to an lvalue reference to const in some expressions. This could result in creating an optional lvalue reference that is in fact bound to an unexpected temporary rather than to the intended object. In order to prevent hard to find run-time bugs, this library performs compile-time checks to prevent expressions that would otherwise bind an optional reference to an unexpected temporary. As a consequence, on certain compilers certain pieces of functionality in optional references are missing. In order to maintain a portability of your code across diferent compilers, it is recommended that you only stick to the minimum portable interface of optional references: prefer direct-initialization and copy assignment of optional references to copy-initialization and assignment from `T&`:
  48. const int i = 0;
  49. optional<const int&> or1;
  50. optional<const int&> or2 = i; // caution: not portable
  51. or1 = i; // caution: not portable
  52. optional<const int&> or3(i); // portable
  53. or1 = optional<const int&>(i); // portable
  54. Compilers known to have these deficiencies include GCC versions 4.2, 4.3, 4.4, 4.5, 5.1, 5.2; QCC 4.4.2; MSVC versions 8.0, 9.0, 10.0, 11.0, 12.0. In order to check if your compiler correctly implements reference binding use this test program.
  55. #include <cassert>
  56. const int global_i = 0;
  57. struct TestingReferenceBinding
  58. {
  59. TestingReferenceBinding(const int& ii)
  60. {
  61. assert(&ii == &global_i);
  62. }
  63. void operator=(const int& ii)
  64. {
  65. assert(&ii == &global_i);
  66. }
  67. void operator=(int&&) // remove this if your compiler doesn't have rvalue refs
  68. {
  69. assert(false);
  70. }
  71. };
  72. int main()
  73. {
  74. const int& iref = global_i;
  75. assert(&iref == &global_i);
  76. TestingReferenceBinding ttt = global_i;
  77. ttt = global_i;
  78. TestingReferenceBinding ttt2 = iref;
  79. ttt2 = iref;
  80. }
  81. [endsect]
  82. [endsect]