examples.qbk 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. [/
  2. Copyright 2007 John Maddock.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:examples Examples]
  8. [section:copy An Optimized Version of std::copy]
  9. Demonstrates a version of `std::copy` that uses `__has_trivial_assign` to
  10. determine whether to use `memcpy` to optimise the copy operation
  11. (see [@../../examples/copy_example.cpp copy_example.cpp]):
  12. //
  13. // opt::copy
  14. // same semantics as std::copy
  15. // calls memcpy where appropriate.
  16. //
  17. namespace detail{
  18. template<typename I1, typename I2, bool b>
  19. I2 copy_imp(I1 first, I1 last, I2 out, const boost::__integral_constant<bool, b>&)
  20. {
  21. while(first != last)
  22. {
  23. *out = *first;
  24. ++out;
  25. ++first;
  26. }
  27. return out;
  28. }
  29. template<typename T>
  30. T* copy_imp(const T* first, const T* last, T* out, const boost::__true_type&)
  31. {
  32. memmove(out, first, (last-first)*sizeof(T));
  33. return out+(last-first);
  34. }
  35. }
  36. template<typename I1, typename I2>
  37. inline I2 copy(I1 first, I1 last, I2 out)
  38. {
  39. //
  40. // We can copy with memcpy if T has a trivial assignment operator,
  41. // and if the iterator arguments are actually pointers (this last
  42. // requirement we detect with overload resolution):
  43. //
  44. typedef typename std::iterator_traits<I1>::value_type value_type;
  45. return detail::copy_imp(first, last, out, boost::__has_trivial_assign<value_type>());
  46. }
  47. [endsect]
  48. [section:fill An Optimised Version of std::fill]
  49. Demonstrates a version of `std::fill` that uses `__has_trivial_assign` to
  50. determine whether to use `memset` to optimise the fill operation
  51. (see [@../../examples/fill_example.cpp fill_example.cpp]):
  52. //
  53. // fill
  54. // same as std::fill, but uses memset where appropriate
  55. //
  56. namespace detail{
  57. template <typename I, typename T, bool b>
  58. void do_fill(I first, I last, const T& val, const boost::__integral_constant<bool, b>&)
  59. {
  60. while(first != last)
  61. {
  62. *first = val;
  63. ++first;
  64. }
  65. }
  66. template <typename T>
  67. void do_fill(T* first, T* last, const T& val, const boost::__true_type&)
  68. {
  69. std::memset(first, val, last-first);
  70. }
  71. }
  72. template <class I, class T>
  73. inline void fill(I first, I last, const T& val)
  74. {
  75. //
  76. // We can do an optimised fill if T has a trivial assignment
  77. // operator and if it's size is one:
  78. //
  79. typedef boost::__integral_constant<bool,
  80. ::boost::__has_trivial_assign<T>::value && (sizeof(T) == 1)> truth_type;
  81. detail::do_fill(first, last, val, truth_type());
  82. }
  83. [endsect]
  84. [section:destruct An Example that Omits Destructor Calls For Types with Trivial Destructors]
  85. Demonstrates a simple algorithm that uses `__has_trivial_destruct` to
  86. determine whether to destructors need to be called
  87. (see [@../../examples/trivial_destructor_example.cpp trivial_destructor_example.cpp]):
  88. //
  89. // algorithm destroy_array:
  90. // The reverse of std::unitialized_copy, takes a block of
  91. // initialized memory and calls destructors on all objects therein.
  92. //
  93. namespace detail{
  94. template <class T>
  95. void do_destroy_array(T* first, T* last, const boost::__false_type&)
  96. {
  97. while(first != last)
  98. {
  99. first->~T();
  100. ++first;
  101. }
  102. }
  103. template <class T>
  104. inline void do_destroy_array(T* first, T* last, const boost::__true_type&)
  105. {
  106. }
  107. } // namespace detail
  108. template <class T>
  109. inline void destroy_array(T* p1, T* p2)
  110. {
  111. detail::do_destroy_array(p1, p2, ::boost::__has_trivial_destructor<T>());
  112. }
  113. [endsect]
  114. [section:iter An improved Version of std::iter_swap]
  115. Demonstrates a version of `std::iter_swap` that use type traits to
  116. determine whether an it's arguments are proxy iterators or not,
  117. if they're not then it just does a `std::swap` of it's dereferenced
  118. arguments (the
  119. same as `std::iter_swap` does), however if they are proxy iterators
  120. then takes special care over the swap to ensure that the algorithm
  121. works correctly for both proxy iterators, and even iterators of
  122. different types
  123. (see [@../../examples/iter_swap_example.cpp iter_swap_example.cpp]):
  124. //
  125. // iter_swap:
  126. // tests whether iterator is a proxy iterator or not, and
  127. // uses optimal form accordingly:
  128. //
  129. namespace detail{
  130. template <typename I>
  131. static void do_swap(I one, I two, const boost::__false_type&)
  132. {
  133. typedef typename std::iterator_traits<I>::value_type v_t;
  134. v_t v = *one;
  135. *one = *two;
  136. *two = v;
  137. }
  138. template <typename I>
  139. static void do_swap(I one, I two, const boost::__true_type&)
  140. {
  141. using std::swap;
  142. swap(*one, *two);
  143. }
  144. }
  145. template <typename I1, typename I2>
  146. inline void iter_swap(I1 one, I2 two)
  147. {
  148. //
  149. // See is both arguments are non-proxying iterators,
  150. // and if both iterator the same type:
  151. //
  152. typedef typename std::iterator_traits<I1>::reference r1_t;
  153. typedef typename std::iterator_traits<I2>::reference r2_t;
  154. typedef boost::__integral_constant<bool,
  155. ::boost::__is_reference<r1_t>::value
  156. && ::boost::__is_reference<r2_t>::value
  157. && ::boost::__is_same<r1_t, r2_t>::value> truth_type;
  158. detail::do_swap(one, two, truth_type());
  159. }
  160. [endsect]
  161. [section:to_double Convert Numeric Types and Enums to double]
  162. Demonstrates a conversion of
  163. [@../../../../libs/numeric/conversion/doc/html/boost_numericconversion/definitions.html#boost_numericconversion.definitions.numeric_types
  164. Numeric Types]
  165. and enum types to double:
  166. template<class T>
  167. inline double to_double(T const& value)
  168. {
  169. typedef typename boost::promote<T>::type promoted;
  170. return boost::numeric::converter<double,promoted>::convert(value);
  171. }
  172. [endsect]
  173. [section:improved_min Improving std::min with common_type]
  174. An improved `std::min` function could be written like this:
  175. template <class T, class U>
  176. typename __common_type<T, U>::type min(T t, U u)
  177. {
  178. return t < u ? t : u;
  179. }
  180. And now expressions such as:
  181. min(1, 2.0)
  182. will actually compile and return the correct type!
  183. [endsect]
  184. [endsect]