first_scalar.qbk 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. [/
  2. Copyright 2019 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:first_scalar first_scalar]
  8. [simplesect Authors]
  9. * Glen Fernandes
  10. [endsimplesect]
  11. [section Overview]
  12. The header <boost/core/first_scalar.hpp> provides the function templates
  13. `boost::first_scalar` that can be used to obtain a pointer to the first scalar
  14. element of an array. Given a pointer of type `T*` they return a pointer of
  15. type `remove_all_extents_t<T>*`. The functions are `constexpr` and can be used
  16. in constant expressions.
  17. [endsect]
  18. [section Examples]
  19. The following function uses an allocator to allocate an array of arrays and
  20. constructs each scalar element in it.
  21. ```
  22. #include <boost/alloc_construct.hpp>
  23. #include <boost/first_scalar.hpp>
  24. template<class A>
  25. auto create(const A& allocator)
  26. {
  27. typename std::allocator_traits<A>::template
  28. rebind_alloc<int[2][3]> other(allocator);
  29. auto ptr = other.allocate(4);
  30. try {
  31. boost::alloc_construct_n(other,
  32. boost::first_scalar(boost::to_address(ptr)), 24);
  33. } catch (...) {
  34. other.deallocate(ptr, 4);
  35. throw;
  36. }
  37. return ptr;
  38. }
  39. ```
  40. [endsect]
  41. [section Reference]
  42. ```
  43. namespace boost {
  44. template<class T>
  45. constexpr T* first_scalar(T* p) noexcept;
  46. template<class T, std::size_t N>
  47. constexpr auto first_scalar(T (*p)[N]) noexcept;
  48. } /* boost */
  49. ```
  50. [section Functions]
  51. [variablelist
  52. [[`template<class T> constexpr T* first_scalar(T* p) noexcept;`]
  53. [[variablelist
  54. [[Returns][`p`.]]]]]
  55. [[`template<class T, std::size_t N> constexpr auto first_scalar(T (*p)[N])
  56. noexcept;`]
  57. [[variablelist
  58. [[Returns][`first_scalar(&(*p)[0])`.]]]]]]
  59. [endsect]
  60. [endsect]
  61. [section History]
  62. Glen Fernandes implemented `first_scalar`. Peter Dimov suggested a change for
  63. GCC to support an additional `constexpr` use.
  64. [endsect]
  65. [endsect]