empty_value.qbk 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. [/
  2. Copyright 2018 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. ]
  7. [section:empty_value empty_value]
  8. [simplesect Authors]
  9. * Glen Fernandes
  10. [endsimplesect]
  11. [section Overview]
  12. The header <boost/core/empty_value.hpp> provides the class template
  13. `boost::empty_value` for library authors to conveniently leverage the Empty
  14. Base Optimization to store objects of potentially empty types.
  15. [endsect]
  16. [section Examples]
  17. The following example shows `boost::empty_value` used to create a type that
  18. stores a pointer, comparer, and allocator, where the comparer and allocator
  19. could be empty types.
  20. ```
  21. template<class Ptr, class Compare, class Allocator>
  22. class storage
  23. : empty_value<Compare, 0>
  24. , empty_value<Allocator, 1> {
  25. public:
  26. storage()
  27. : empty_value<Compare, 0>(empty_init_t())
  28. , empty_value<Allocator, 1>(empty_init_t())
  29. , ptr_() { }
  30. storage(const Compare& c, const Allocator& a)
  31. : empty_value<Compare, 0>(empty_init_t(), c)
  32. , empty_value<Allocator, 1>(empty_init_t(), a)
  33. , ptr_() { }
  34. const Ptr& pointer() const {
  35. return ptr_;
  36. }
  37. Ptr& pointer() {
  38. return ptr_;
  39. }
  40. const Compare& compare() const {
  41. return empty_value<Compare, 0>::get();
  42. }
  43. Compare& compare() {
  44. return empty_value<Compare, 0>::get();
  45. }
  46. const Allocator& allocator() const {
  47. return empty_value<Allocator, 1>::get();
  48. }
  49. Allocator& allocator() {
  50. return empty_value<Allocator, 1>::get();
  51. }
  52. private:
  53. Ptr ptr_;
  54. };
  55. ```
  56. [endsect]
  57. [section Reference]
  58. ```
  59. namespace boost {
  60. struct empty_init_t { };
  61. template<class T, unsigned Index = 0, bool Empty = ``/see below/``>
  62. class empty_value {
  63. public:
  64. typedef T type;
  65. empty_value() = default;
  66. template<class... Args>
  67. explicit empty_value(empty_init_t, Args&&... args);
  68. const T& get() const noexcept;
  69. T& get() noexcept;
  70. };
  71. } /* boost */
  72. ```
  73. [section Template parameters]
  74. [variablelist
  75. [[`T`][The type of value to store]]
  76. [[`Index`][Optional: Specify to create a distinct base type]]
  77. [[`Empty`][Optional: Specify to force inheritance from type]]]
  78. [endsect]
  79. [section Member types]
  80. [variablelist
  81. [[`type`][The template parameter `T`]]]
  82. [endsect]
  83. [section Constructors]
  84. [variablelist
  85. [[`empty_value() = default;`][Default initialize the value]]
  86. [[`template<class... Args> empty_value(empty_init_t, Args&&... args);`]
  87. [Initialize the value with `std::forward<Args>(args)...`]]]
  88. [endsect]
  89. [section Member functions]
  90. [variablelist
  91. [[`const T& get() const noexcept;`][Returns the value]]
  92. [[`T& get() noexcept;`][Returns the value]]]
  93. [endsect]
  94. [endsect]
  95. [endsect]